zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 9ec4ccc68f6473ce4f02fc5d2b87ad92e2eb555a (tree)
parent 4df2f3d74f140ae6a60dc33d3e0fb7b25f29d5b9
Author: LemonBoy <thatlemon@gmail.com>
Date:   Fri, 26 Apr 2019 15:57:47 +0200

Do not invoke UB in BigInt shr operations

Shifting a value of type T by an amount that's greater or equal to the
size of the type itself is UB.

Spotted by @tgschultz

Diffstat:
Msrc/bigint.cpp | 2+-
Mtest/stage1/behavior/bit_shifting.zig | 6++++--
2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/src/bigint.cpp b/src/bigint.cpp @@ -1395,7 +1395,7 @@ void bigint_shr(BigInt *dest, const BigInt *op1, const BigInt *op2) { uint64_t shift_amt = bigint_as_unsigned(op2); if (op1->digit_count == 1) { - dest->data.digit = op1_digits[0] >> shift_amt; + dest->data.digit = (shift_amt < 64) ? op1_digits[0] >> shift_amt : 0; dest->digit_count = 1; dest->is_negative = op1->is_negative; bigint_normalize(dest); diff --git a/test/stage1/behavior/bit_shifting.zig b/test/stage1/behavior/bit_shifting.zig @@ -90,7 +90,9 @@ fn testShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, c // #2225 test "comptime shr of BigInt" { comptime { - var n = 0xdeadbeef0000000000000000; - std.debug.assert(n >> 64 == 0xdeadbeef); + var n0 = 0xdeadbeef0000000000000000; + std.debug.assert(n0 >> 64 == 0xdeadbeef); + var n1 = 17908056155735594659; + std.debug.assert(n1 >> 64 == 0); } }