From ab0562950c36af3040b48ab4e8494da97505677c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Mon, 21 Feb 2022 13:53:52 +0200 Subject: [PATCH] add some guard rails w.r.t. padding --- src/user.zig | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/user.zig b/src/user.zig index 47b3241..387fdf5 100644 --- a/src/user.zig +++ b/src/user.zig @@ -199,8 +199,19 @@ pub const UserReader = struct { const testing = std.testing; -test "PackedUser is byte-aligned" { +test "PackedUser alignment" { + // byte-aligned try testing.expectEqual(0, @rem(@bitSizeOf(PackedUser), 8)); + + const bytes = @divExact(@bitSizeOf(PackedUser), 8); + + // External padding (PackedUserAlignmentBits) must be higher or equal to + // the "internal" PackedUser alignment. By aligning PackedUser we are also + // working around https://github.com/ziglang/zig/issues/10958 ; PackedUser + // cannot be converted from/to [@bitSizeOf(PackedUser)/8]u8; + // asBytes/bytesAsValue use @sizeOf, which is larger. Now we are putting no + // more than 1, but it probably could be higher. + try testing.expect(@sizeOf(PackedUser) - bytes <= 1); } fn testShellIndex(shell: []const u8) ?u6 {