getgrnam works, except for memory leaks

This commit is contained in:
Motiejus Jakštys 2022-04-17 08:58:57 +03:00 committed by Motiejus Jakštys
parent 67360962c3
commit 28e8f11f91

View File

@ -256,22 +256,23 @@ fn groupMemberNames(
arr[num_members] = null; arr[num_members] = null;
arr.len = num_members; arr.len = num_members;
var buf = try allocator.alloc(u8, total_members_len + num_members); var buf = std.ArrayList(u8).init(allocator);
errdefer allocator.free(buf); // +num_members are for sentinel zeroes
try buf.ensureTotalCapacity(total_members_len + num_members);
var it = compress.DeltaDecompressionIterator(&vit); var it = compress.DeltaDecompressionIterator(&vit);
var i: usize = 0; var i: usize = 0;
while (it.nextMust()) |member_offset| : (i += 1) { while (it.nextMust()) |member_offset| : (i += 1) {
const entry = PackedUser.fromBytes(self.users[member_offset..]); const entry = PackedUser.fromBytes(self.users[member_offset << 3 ..]);
const start = buf.items.len;
const name = entry.user.name(); const name = entry.user.name();
const old_len = buf.len; buf.appendSliceAssumeCapacity(name);
buf.len += name.len + 1; buf.appendAssumeCapacity(0);
mem.copy(u8, buf[old_len..], name); // TODO: arr[i] = buf[...] triggers a bug in zig pre-0.10
buf[buf.len - 1] = 0; const terminated = buf.items[start .. buf.items.len - 1 :0];
// TODO: arr[i] = buf[...] triggers a bug in zig compiler
const terminated = buf[old_len..buf.len :0];
arr[i] = terminated; arr[i] = terminated;
} }
return GroupMemberNames{ ._buf = buf, .arr = arr }; return GroupMemberNames{ ._buf = buf.toOwnedSlice(), .arr = arr };
} }
// getgrtnam returns a Group entry by name. The Group must be // getgrtnam returns a Group entry by name. The Group must be
@ -283,7 +284,7 @@ fn getgrnam(
) error{OutOfMemory}!?CGroup { ) error{OutOfMemory}!?CGroup {
const idx = bdz.search(self.bdz_groupname, name); const idx = bdz.search(self.bdz_groupname, name);
const offset = self.idx_groupname2group[idx]; const offset = self.idx_groupname2group[idx];
const group = PackedGroup.fromBytes(self.groups[offset..]).group; const group = PackedGroup.fromBytes(self.groups[offset << 3 ..]).group;
if (!mem.eql(u8, name, group.name())) return null; if (!mem.eql(u8, name, group.name())) return null;
var members = try self.groupMemberNames(allocator, group.members_offset); var members = try self.groupMemberNames(allocator, group.members_offset);
@ -516,6 +517,7 @@ fn bdzIdx(
}; };
assert(keys.len <= math.maxInt(u32)); assert(keys.len <= math.maxInt(u32));
var result = try allocator.alloc(u32, keys.len); var result = try allocator.alloc(u32, keys.len);
errdefer allocator.free(result);
for (keys) |key, i| for (keys) |key, i|
result[search_fn(packed_mphf, key)] = idx2offset[i]; result[search_fn(packed_mphf, key)] = idx2offset[i];
return result; return result;
@ -623,7 +625,9 @@ test "high-level API" {
defer db.deinit(allocator); defer db.deinit(allocator);
const all = try db.getgrnam(allocator, "all"); const all = try db.getgrnam(allocator, "all");
_ = all; try testing.expect(all != null);
try testing.expectEqual(all.?.gid, 9999);
try testing.expectEqualStrings(all.?.name, "all");
} }
test "additionalGids" { test "additionalGids" {