fix Group.init to copy appropriately
This commit is contained in:
parent
5968d8494e
commit
67360962c3
@ -2,5 +2,6 @@ const CGroup = @This();
|
|||||||
|
|
||||||
gid: u32,
|
gid: u32,
|
||||||
name: [:0]const u8,
|
name: [:0]const u8,
|
||||||
|
// Should be a sentinel-terminated array.
|
||||||
// https://github.com/ziglang/zig/issues/7517
|
// https://github.com/ziglang/zig/issues/7517
|
||||||
members: [:null]const ?[*:0]const u8,
|
members: []const ?[*:0]const u8,
|
||||||
|
@ -203,17 +203,18 @@ pub fn testCorpus(allocator: Allocator) !Corpus {
|
|||||||
.shell = "/usr/sbin/nologin",
|
.shell = "/usr/sbin/nologin",
|
||||||
} };
|
} };
|
||||||
|
|
||||||
var members0 = &[_][]const u8{"root"};
|
var group0 = try Group.init(allocator, 0, "root", &[_][]const u8{"root"});
|
||||||
var members1 = &[_][]const u8{"vidmantas"};
|
var group1 = try Group.init(allocator, 128, "vidmantas", &[_][]const u8{"vidmantas"});
|
||||||
var members2 = &[_][]const u8{ "svc-bar", "vidmantas" };
|
const members2 = &[_][]const u8{ "svc-bar", "Name" ** 8, "vidmantas", "root" };
|
||||||
var members3 = &[_][]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{
|
const groups = [_]Group{ group0, group1, group2, group3 };
|
||||||
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),
|
|
||||||
};
|
|
||||||
|
|
||||||
return try Corpus.init(allocator, users[0..], groups[0..]);
|
return try Corpus.init(allocator, users[0..], groups[0..]);
|
||||||
}
|
}
|
||||||
|
@ -221,7 +221,7 @@ pub fn fromBytes(buf: []align(8) const u8) InvalidHeader!DB {
|
|||||||
|
|
||||||
const GroupMemberNames = struct {
|
const GroupMemberNames = struct {
|
||||||
_buf: []u8,
|
_buf: []u8,
|
||||||
arr: [:null]const ?[*:0]const u8,
|
arr: []const ?[*:0]const u8,
|
||||||
|
|
||||||
pub fn deinit(self: *GroupMemberNames, allocator: Allocator) void {
|
pub fn deinit(self: *GroupMemberNames, allocator: Allocator) void {
|
||||||
if (self._buf.len == 0) return;
|
if (self._buf.len == 0) return;
|
||||||
@ -245,7 +245,7 @@ fn groupMemberNames(
|
|||||||
if (num_members == 0)
|
if (num_members == 0)
|
||||||
return GroupMemberNames{
|
return GroupMemberNames{
|
||||||
._buf = &[0]u8{},
|
._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
|
// TODO (zig 0.10+) make result type sentinel-aware and stop
|
||||||
@ -267,7 +267,9 @@ fn groupMemberNames(
|
|||||||
buf.len += name.len + 1;
|
buf.len += name.len + 1;
|
||||||
mem.copy(u8, buf[old_len..], name);
|
mem.copy(u8, buf[old_len..], name);
|
||||||
buf[buf.len - 1] = 0;
|
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 };
|
return GroupMemberNames{ ._buf = buf, .arr = arr };
|
||||||
}
|
}
|
||||||
|
@ -23,23 +23,22 @@ pub fn init(
|
|||||||
const buf_len = @sizeOf([]const u8) * members.len + name.len + blk: {
|
const buf_len = @sizeOf([]const u8) * members.len + name.len + blk: {
|
||||||
var sum: usize = 0;
|
var sum: usize = 0;
|
||||||
for (members) |member| sum += member.len;
|
for (members) |member| sum += member.len;
|
||||||
std.debug.print("\nsum: {d}\n", .{sum});
|
|
||||||
break :blk @intCast(u32, sum);
|
break :blk @intCast(u32, sum);
|
||||||
};
|
};
|
||||||
var buf = try allocator.alloc(u8, buf_len);
|
var buf = try allocator.alloc(u8, buf_len);
|
||||||
errdefer allocator.free(buf);
|
errdefer allocator.free(buf);
|
||||||
var ptr_end = @sizeOf([]const u8) * members.len;
|
var ptr_end = @sizeOf([]const u8) * members.len;
|
||||||
var members_ptr = mem.bytesAsSlice([]const u8, buf[0..ptr_end]);
|
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| {
|
for (members) |member, i| {
|
||||||
mem.copy(u8, buf[start..], member);
|
mem.copy(u8, buf[offset..], member);
|
||||||
members_ptr[i] = buf[start .. start + member.len];
|
members_ptr[i] = buf[offset .. offset + member.len];
|
||||||
start += member.len;
|
offset += member.len;
|
||||||
}
|
}
|
||||||
mem.copy(u8, buf[start..], name);
|
mem.copy(u8, buf[offset..], name);
|
||||||
return Group{
|
return Group{
|
||||||
.gid = gid,
|
.gid = gid,
|
||||||
.name = buf[start .. start + name.len],
|
.name = buf[offset .. offset + name.len],
|
||||||
.members = members_ptr,
|
.members = members_ptr,
|
||||||
._buf = buf,
|
._buf = buf,
|
||||||
};
|
};
|
||||||
@ -63,6 +62,7 @@ test "Group.clone" {
|
|||||||
defer testing.allocator.free(member1);
|
defer testing.allocator.free(member1);
|
||||||
|
|
||||||
var group = try init(testing.allocator, 1, "foo", &[_][]const u8{ member1, "member2" });
|
var group = try init(testing.allocator, 1, "foo", &[_][]const u8{ member1, "member2" });
|
||||||
|
defer group.deinit(testing.allocator);
|
||||||
|
|
||||||
var cloned = try group.clone(testing.allocator);
|
var cloned = try group.clone(testing.allocator);
|
||||||
defer cloned.deinit(testing.allocator);
|
defer cloned.deinit(testing.allocator);
|
||||||
|
Loading…
Reference in New Issue
Block a user