1
Fork 0

fix PackedUser.maxSize

main
Motiejus Jakštys 2022-02-25 15:17:24 +02:00 committed by Motiejus Jakštys
parent b0d74c0cad
commit 22c61d1207
1 changed files with 21 additions and 4 deletions

View File

@ -171,6 +171,7 @@ pub const PackedUser = struct {
.gecos_len = gecos_len,
};
const innerBytes = mem.asBytes(&inner);
var pos: usize = buf.*.len;
buf.*.len += InnerSize +
user.home.len +
@ -207,9 +208,9 @@ pub const PackedUser = struct {
pub fn maxSize() usize {
comptime {
const unpadded = InnerSize +
std.math.maxInt(u6) + // home
std.math.maxInt(u5) + // name
std.math.maxInt(u6) + // shell
std.math.maxInt(u6) + 1 + // home
std.math.maxInt(u5) + 1 + // name
std.math.maxInt(u6) + 1 + // shell
std.math.maxInt(u8); // gecos
return pad.roundUp(u64, AlignmentBits, unpadded);
}
@ -323,7 +324,7 @@ test "construct PackedUser section" {
.name = "Name" ** 8,
.gecos = "Gecos" ** 51,
.home = "Home" ** 16,
.shell = "She.l..l" ** 8,
.shell = "She.LllL" ** 8,
}, User{
.uid = 1002,
.gid = 1002,
@ -349,3 +350,19 @@ test "construct PackedUser section" {
}
try testing.expectEqual(users.len, i);
}
test "PackedUser.maxSize()" {
var buf = try ArrayList(u8).initCapacity(testing.allocator, PackedUser.maxSize());
defer buf.deinit();
const largeUser = User{
.uid = math.maxInt(u32),
.gid = math.maxInt(u32),
.name = "Name" ** 8, // 32
.gecos = "Gecos" ** 51, // 255
.home = "Home" ** 16, // 64
.shell = "She.LllL" ** 8, // 64
};
try PackedUser.packTo(&buf.items, largeUser, testShellIndex);
try testing.expectEqual(PackedUser.maxSize(), buf.items.len);
}