diff --git a/lib/DB.zig b/lib/DB.zig index 3a30c21..a4ee78a 100644 --- a/lib/DB.zig +++ b/lib/DB.zig @@ -76,6 +76,12 @@ pub fn fromCorpus( var shell = try shellSections(allocator, corpus); defer shell.deinit(); + const shell_index = try allocator.dupe(u16, shell.index.constSlice()); + errdefer allocator.free(shell_index); + + const shell_blob = try allocator.dupe(u8, shell.blob.constSlice()); + errdefer allocator.free(shell_blob); + const additional_gids = try additionalGids(allocator, corpus); errdefer allocator.free(additional_gids.blob); defer allocator.free(additional_gids.idx2offset); @@ -124,8 +130,6 @@ pub fn fromCorpus( .getpw_bufsize = corpus.getpw_bufsize, }; - std.debug.print("fromCorpus shell.index: {any}\n", .{shell.index.constSlice()}); - return DB{ .header = header, .bdz_gid = bdz_gid, @@ -136,8 +140,8 @@ pub fn fromCorpus( .idx_groupname2group = idx_groupname2group, .idx_uid2user = idx_uid2user, .idx_name2user = idx_name2user, - .shell_index = shell.index.constSlice(), - .shell_blob = shell.blob.constSlice(), + .shell_index = shell_index, + .shell_blob = shell_blob, .groups = groups.blob, .users = users.blob, .groupmembers = groupmembers.blob, @@ -155,6 +159,8 @@ pub fn deinit(self: *DB, allocator: Allocator) void { allocator.free(self.idx_groupname2group); allocator.free(self.idx_uid2user); allocator.free(self.idx_name2user); + allocator.free(self.shell_index); + allocator.free(self.shell_blob); allocator.free(self.groups); allocator.free(self.users); allocator.free(self.groupmembers); @@ -172,8 +178,6 @@ pub fn iov(self: *const DB) BoundedArray(os.iovec_const, DB_fields.len * 2) { *const Header => mem.asBytes(value), else => mem.sliceAsBytes(value), }; - if (mem.eql(u8, field.name, "shell_index")) - std.debug.print("shell_index original: {any}\n", .{value}); result.appendAssumeCapacity(os.iovec_const{ .iov_base = bytes.ptr, .iov_len = bytes.len, @@ -224,8 +228,6 @@ pub fn fromBytes(buf: []align(8) const u8) InvalidHeader!DB { offset += lengths[i]; } - std.debug.print("GOT shell_index: {any}\n", .{result.shell_index}); - return result; } @@ -652,34 +654,25 @@ test "getgrnam/getgrgid" { try testing.expectEqual(all.gid, 9999); try testing.expectEqualStrings(all.name[0..3], "all"); } -} - -test "getgr_bufsize" { - var arena = ArenaAllocator.init(testing.allocator); - defer arena.deinit(); - var corpus = try Corpus.testCorpus(arena.allocator()); - var db = try DB.fromCorpus(arena.allocator(), &corpus); - var buf = try arena.allocator().alloc(u8, db.header.getgr_bufsize); _ = try db.getgrnam("all", &buf); buf.len -= 1; try testing.expectError(error.OutOfMemory, db.getgrnam("all", &buf)); } -test "getpwnam" { +test "getpwnam/getpwuid" { var arena = ArenaAllocator.init(testing.allocator); defer arena.deinit(); var corpus = try Corpus.testCorpus(arena.allocator()); var db = try DB.fromCorpus(arena.allocator(), &corpus); - std.debug.print("db.shell.index: {any}\n", .{db.shell_index}); var buf = try arena.allocator().alloc(u8, db.header.getpw_bufsize); const vidmantas = (try db.getpwnam("vidmantas", &buf)).?; try testing.expectEqual(vidmantas.pw_uid, 128); try testing.expectEqual(vidmantas.pw_gid, 128); try testing.expectEqualStrings(vidmantas.pw_name[0..9], "vidmantas"); - try testing.expectEqualStrings(vidmantas.pw_gecos[0..10], "Vidmantas Kaminskas"); - try testing.expectEqualStrings(vidmantas.pw_dir[0..10], "/home/vidmantas"); + try testing.expectEqualStrings(vidmantas.pw_gecos[0..19], "Vidmantas Kaminskas"); + try testing.expectEqualStrings(vidmantas.pw_dir[0..15], "/home/vidmantas"); } test "additionalGids" { diff --git a/lib/shell.zig b/lib/shell.zig index 8374a8d..9c4b6e6 100644 --- a/lib/shell.zig +++ b/lib/shell.zig @@ -22,8 +22,6 @@ pub const ShellReader = struct { // get returns a shell at the given index. pub fn get(self: *const ShellReader, idx: u8) []const u8 { - std.debug.print("\nrequested shell at index {d}; ", .{idx}); - std.debug.print("start:{d} end:{d}\n", .{ self.index[idx], self.index[idx + 1] }); return self.blob[self.index[idx]..self.index[idx + 1]]; } }; @@ -75,18 +73,9 @@ pub const ShellWriter = struct { self.index.set(idx8, offset); } self.index.appendAssumeCapacity(@intCast(u8, self.blob.len)); - std.debug.print("\nGEN shell_index: {any}\n", .{self.index.constSlice()}); return self; } - pub fn section_index(self: *const ShellSections) []align(2) const u8 { - return std.mem.sliceAsBytes(self.index.constSlice()); - } - - pub fn section_blob(self: *const ShellSections) []const u8 { - return self.blob.constSlice(); - } - pub fn deinit(self: *ShellSections) void { self.shell2idx.deinit(); self.* = undefined; @@ -184,11 +173,11 @@ test "basic shellpopcon" { try testing.expectEqual(sections.getIndex(zsh).?, 1); try testing.expectEqual(sections.getIndex(bash).?, 2); try testing.expectEqual(sections.getIndex(nobody), null); - try testing.expectEqual(sections.section_blob().len, bash.len + zsh.len + long.len); + try testing.expectEqual(sections.blob.constSlice().len, bash.len + zsh.len + long.len); const shellReader = ShellReader.init( - sections.section_index(), - sections.section_blob(), + std.mem.sliceAsBytes(sections.index.constSlice()), + sections.blob.constSlice(), ); try testing.expectEqualStrings(shellReader.get(0), long); try testing.expectEqualStrings(shellReader.get(1), zsh);