diff options
author | 2023-09-24 11:09:45 +0800 | |
---|---|---|
committer | 2023-09-23 20:09:45 -0700 | |
commit | a5908e9f2704f801587115007c2bec5788bd718a (patch) | |
tree | 5ef841abad46365ac826135291b011d43694b1dd /src | |
parent | 72f9017b21cbb6de83d054a0b985e9fe9a7708f3 (diff) | |
download | bun-a5908e9f2704f801587115007c2bec5788bd718a.tar.gz bun-a5908e9f2704f801587115007c2bec5788bd718a.tar.zst bun-a5908e9f2704f801587115007c2bec5788bd718a.zip |
fix(lockfile): ensure all bytes of union are initialized before serialization. (#5957)
Diffstat (limited to 'src')
-rw-r--r-- | src/install/bin.zig | 41 |
1 files changed, 23 insertions, 18 deletions
diff --git a/src/install/bin.zig b/src/install/bin.zig index 0a8d62c8c..7c429f5d0 100644 --- a/src/install/bin.zig +++ b/src/install/bin.zig @@ -20,7 +20,7 @@ pub const Bin = extern struct { tag: Tag = Tag.none, _padding_tag: [3]u8 = .{0} ** 3, - value: Value = Value{ .none = {} }, + value: Value = std.mem.zeroes(Value), pub fn verify(this: *const Bin, extern_strings: []const ExternalString) void { if (comptime !Environment.allow_assert) @@ -67,36 +67,41 @@ pub const Bin = extern struct { } pub fn clone(this: *const Bin, buf: []const u8, prev_external_strings: []const ExternalString, all_extern_strings: []ExternalString, extern_strings_slice: []ExternalString, comptime StringBuilder: type, builder: StringBuilder) Bin { - return switch (this.tag) { - .none => Bin{ .tag = .none, .value = .{ .none = {} } }, - .file => Bin{ - .tag = .file, - .value = .{ .file = builder.append(String, this.value.file.slice(buf)) }, + var cloned: Bin = Bin{}; + @memset(std.mem.asBytes(&cloned), 0); + + switch (this.tag) { + .none => { + cloned.tag = .none; + cloned.value.none = {}; + }, + .file => { + cloned.tag = .file; + cloned.value = .{ .file = builder.append(String, this.value.file.slice(buf)) }; }, - .named_file => Bin{ - .tag = .named_file, - .value = .{ + .named_file => { + cloned.tag = .named_file; + cloned.value = .{ .named_file = [2]String{ builder.append(String, this.value.named_file[0].slice(buf)), builder.append(String, this.value.named_file[1].slice(buf)), }, - }, + }; }, - .dir => Bin{ - .tag = .dir, - .value = .{ .dir = builder.append(String, this.value.dir.slice(buf)) }, + .dir => { + cloned.tag = .dir; + cloned.value = .{ .dir = builder.append(String, this.value.dir.slice(buf)) }; }, .map => { for (this.value.map.get(prev_external_strings), 0..) |extern_string, i| { extern_strings_slice[i] = builder.append(ExternalString, extern_string.slice(buf)); } - return .{ - .tag = .map, - .value = .{ .map = ExternalStringList.init(all_extern_strings, extern_strings_slice) }, - }; + cloned.tag = .map; + cloned.value = .{ .map = ExternalStringList.init(all_extern_strings, extern_strings_slice) }; }, - }; + } + return cloned; } pub const Value = extern union { |