fix Group.init to copy appropriately

This commit is contained in:
Motiejus Jakštys 2022-04-16 06:24:55 +03:00 committed by Motiejus Jakštys
parent 5968d8494e
commit 67360962c3
4 changed files with 25 additions and 21 deletions

View File

@ -2,5 +2,6 @@ const CGroup = @This();
gid: u32,
name: [:0]const u8,
// Should be a sentinel-terminated array.
// https://github.com/ziglang/zig/issues/7517
members: [:null]const ?[*:0]const u8,
members: []const ?[*:0]const u8,

View File

@ -203,17 +203,18 @@ pub fn testCorpus(allocator: Allocator) !Corpus {
.shell = "/usr/sbin/nologin",
} };
var members0 = &[_][]const u8{"root"};
var members1 = &[_][]const u8{"vidmantas"};
var members2 = &[_][]const u8{ "svc-bar", "vidmantas" };
var members3 = &[_][]const u8{ "svc-bar", "Name" ** 8, "vidmantas", "root" };
var group0 = try Group.init(allocator, 0, "root", &[_][]const u8{"root"});
var group1 = try Group.init(allocator, 128, "vidmantas", &[_][]const u8{"vidmantas"});
const members2 = &[_][]const u8{ "svc-bar", "Name" ** 8, "vidmantas", "root" };
var group2 = try Group.init(allocator, 9999, "all", members2);
const members3 = &[_][]const u8{ "svc-bar", "vidmantas" };
var group3 = try Group.init(allocator, 100000, "service-account", members3);
defer group0.deinit(allocator);
defer group1.deinit(allocator);
defer group2.deinit(allocator);
defer group3.deinit(allocator);
const groups = [_]Group{
try Group.init(allocator, 0, "root", members0),
try Group.init(allocator, 128, "vidmantas", members1),
try Group.init(allocator, 9999, "all", members3),
try Group.init(allocator, 100000, "service-account", members2),
};
const groups = [_]Group{ group0, group1, group2, group3 };
return try Corpus.init(allocator, users[0..], groups[0..]);
}

View File

@ -221,7 +221,7 @@ pub fn fromBytes(buf: []align(8) const u8) InvalidHeader!DB {
const GroupMemberNames = struct {
_buf: []u8,
arr: [:null]const ?[*:0]const u8,
arr: []const ?[*:0]const u8,
pub fn deinit(self: *GroupMemberNames, allocator: Allocator) void {
if (self._buf.len == 0) return;
@ -245,7 +245,7 @@ fn groupMemberNames(
if (num_members == 0)
return GroupMemberNames{
._buf = &[0]u8{},
.arr = &[1:null]?[*:0]const u8{null},
.arr = &[1]?[*:0]const u8{null},
};
// TODO (zig 0.10+) make result type sentinel-aware and stop
@ -267,7 +267,9 @@ fn groupMemberNames(
buf.len += name.len + 1;
mem.copy(u8, buf[old_len..], name);
buf[buf.len - 1] = 0;
arr[i] = buf[old_len..buf.len :0];
// TODO: arr[i] = buf[...] triggers a bug in zig compiler
const terminated = buf[old_len..buf.len :0];
arr[i] = terminated;
}
return GroupMemberNames{ ._buf = buf, .arr = arr };
}

View File

@ -23,23 +23,22 @@ pub fn init(
const buf_len = @sizeOf([]const u8) * members.len + name.len + blk: {
var sum: usize = 0;
for (members) |member| sum += member.len;
std.debug.print("\nsum: {d}\n", .{sum});
break :blk @intCast(u32, sum);
};
var buf = try allocator.alloc(u8, buf_len);
errdefer allocator.free(buf);
var ptr_end = @sizeOf([]const u8) * members.len;
var members_ptr = mem.bytesAsSlice([]const u8, buf[0..ptr_end]);
var start: usize = name.len;
var offset: usize = ptr_end;
for (members) |member, i| {
mem.copy(u8, buf[start..], member);
members_ptr[i] = buf[start .. start + member.len];
start += member.len;
mem.copy(u8, buf[offset..], member);
members_ptr[i] = buf[offset .. offset + member.len];
offset += member.len;
}
mem.copy(u8, buf[start..], name);
mem.copy(u8, buf[offset..], name);
return Group{
.gid = gid,
.name = buf[start .. start + name.len],
.name = buf[offset .. offset + name.len],
.members = members_ptr,
._buf = buf,
};
@ -63,6 +62,7 @@ test "Group.clone" {
defer testing.allocator.free(member1);
var group = try init(testing.allocator, 1, "foo", &[_][]const u8{ member1, "member2" });
defer group.deinit(testing.allocator);
var cloned = try group.clone(testing.allocator);
defer cloned.deinit(testing.allocator);