From bfc0c35689cd5781a3493bdd9c9b8d527847c7a2 Mon Sep 17 00:00:00 2001 From: mlugg Date: Tue, 26 Mar 2024 14:10:35 +0000 Subject: [PATCH] Value: fix underflow reading large `u64` values from packed memory --- src/Value.zig | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Value.zig b/src/Value.zig index b1230954ed..f8f23667e2 100644 --- a/src/Value.zig +++ b/src/Value.zig @@ -787,12 +787,12 @@ pub fn readFromPackedMemory( if (bits == 0) return mod.intValue(ty, 0); // Fast path for integers <= u64 - if (bits <= 64) { - return mod.intValue( - ty, - std.mem.readVarPackedInt(i64, buffer, bit_offset, bits, endian, int_info.signedness), - ); - } + if (bits <= 64) switch (int_info.signedness) { + // Use different backing types for unsigned vs signed to avoid the need to go via + // a larger type like `i128`. + .unsigned => return mod.intValue(ty, std.mem.readVarPackedInt(u64, buffer, bit_offset, bits, endian, .unsigned)), + .signed => return mod.intValue(ty, std.mem.readVarPackedInt(i64, buffer, bit_offset, bits, endian, .signed)), + }; // Slow path, we have to construct a big-int const abi_size = @as(usize, @intCast(ty.abiSize(mod)));