From 46f978838ddb4326d9ab089fbce50d47d829a60c Mon Sep 17 00:00:00 2001 From: Dylan Conway <35280289+dylan-conway@users.noreply.github.com> Date: Fri, 13 Oct 2023 20:37:06 -0700 Subject: fix lockfile struct padding (#6495) * integrity padding * error message for bytes at end of struct --- src/install/integrity.zig | 3 +-- src/install/lockfile.zig | 1 + src/install/padding_checker.zig | 17 ++++++++++++++++- 3 files changed, 18 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/install/integrity.zig b/src/install/integrity.zig index fb9a55509..c0b02d4bf 100644 --- a/src/install/integrity.zig +++ b/src/install/integrity.zig @@ -3,8 +3,7 @@ const strings = @import("../string_immutable.zig"); const Crypto = @import("../sha.zig").Hashers; pub const Integrity = extern struct { - // this is zeroed like this to work around a comptime issue. - const empty_digest_buf: [Integrity.digest_buf_len]u8 = [_]u8{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + const empty_digest_buf: [Integrity.digest_buf_len]u8 = [_]u8{0} ** Integrity.digest_buf_len; tag: Tag = Tag.unknown, /// Possibly a [Subresource Integrity](https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity) value initially diff --git a/src/install/lockfile.zig b/src/install/lockfile.zig index 87d19742b..c1904e962 100644 --- a/src/install/lockfile.zig +++ b/src/install/lockfile.zig @@ -4120,6 +4120,7 @@ pub const Package = extern struct { man_dir: String = String{}, integrity: Integrity = Integrity{}, + _padding_integrity: [3]u8 = .{0} ** 3, /// Does the `cpu` arch and `os` match the requirements listed in the package? /// This is completely unrelated to "devDependencies", "peerDependencies", "optionalDependencies" etc diff --git a/src/install/padding_checker.zig b/src/install/padding_checker.zig index 1d9405a43..52d343b4f 100644 --- a/src/install/padding_checker.zig +++ b/src/install/padding_checker.zig @@ -55,7 +55,6 @@ pub fn assertNoUninitializedPadding(comptime T: type) void { // if (info.layout != .Extern) { // @compileError("assertNoUninitializedPadding(" ++ @typeName(T) ++ ") expects an extern struct type, got a struct of layout '" ++ @tagName(info.layout) ++ "'"); // } - var i = 0; for (info.fields) |field| { const fieldInfo = @typeInfo(field.type); switch (fieldInfo) { @@ -69,9 +68,12 @@ pub fn assertNoUninitializedPadding(comptime T: type) void { else => {}, } } + if (info_ == .Union) { return; } + + var i = 0; for (info.fields, 0..) |field, j| { const offset = @offsetOf(T, field.name); if (offset != i) { @@ -90,4 +92,17 @@ pub fn assertNoUninitializedPadding(comptime T: type) void { } i = offset + @sizeOf(field.type); } + + if (i != @sizeOf(T)) { + @compileError(std.fmt.comptimePrint( + \\Expected no possibly uninitialized bytes of memory in '{s}', but found a {d} byte gap at the end of the struct. This can be fixed by adding a padding field to the struct like `padding: [{d}]u8 = .{{0}} ** {d},` between these fields. For more information, look at `padding_checker.zig` + , + .{ + @typeName(T), + @sizeOf(T) - i, + @sizeOf(T) - i, + @sizeOf(T) - i, + }, + )); + } } -- cgit v1.2.3