mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 05:44:20 +00:00
big.int: fix negative multi-limb shift right adjust crash
This commit is contained in:
parent
8e15321c7c
commit
aff2be01c9
2 changed files with 13 additions and 10 deletions
|
|
@ -1096,7 +1096,7 @@ pub const Mutable = struct {
|
|||
/// Asserts there is enough memory to fit the result. The upper bound Limb count is
|
||||
/// `a.limbs.len + (shift / (@sizeOf(Limb) * 8))`.
|
||||
pub fn shiftLeft(r: *Mutable, a: Const, shift: usize) void {
|
||||
llshl(r.limbs[0..], a.limbs[0..a.limbs.len], shift);
|
||||
llshl(r.limbs, a.limbs, shift);
|
||||
r.normalize(a.limbs.len + (shift / limb_bits) + 1);
|
||||
r.positive = a.positive;
|
||||
}
|
||||
|
|
@ -1165,7 +1165,7 @@ pub const Mutable = struct {
|
|||
|
||||
// This shift should not be able to overflow, so invoke llshl and normalize manually
|
||||
// to avoid the extra required limb.
|
||||
llshl(r.limbs[0..], a.limbs[0..a.limbs.len], shift);
|
||||
llshl(r.limbs, a.limbs, shift);
|
||||
r.normalize(a.limbs.len + (shift / limb_bits));
|
||||
r.positive = a.positive;
|
||||
}
|
||||
|
|
@ -1202,17 +1202,11 @@ pub const Mutable = struct {
|
|||
break :nonzero a.limbs[full_limbs_shifted_out] << not_covered != 0;
|
||||
};
|
||||
|
||||
llshr(r.limbs[0..], a.limbs[0..a.limbs.len], shift);
|
||||
llshr(r.limbs, a.limbs, shift);
|
||||
|
||||
r.len = a.limbs.len - full_limbs_shifted_out;
|
||||
r.positive = a.positive;
|
||||
if (nonzero_negative_shiftout) {
|
||||
if (full_limbs_shifted_out > 0) {
|
||||
r.limbs[a.limbs.len - full_limbs_shifted_out] = 0;
|
||||
r.len += 1;
|
||||
}
|
||||
r.addScalar(r.toConst(), -1);
|
||||
}
|
||||
if (nonzero_negative_shiftout) r.addScalar(r.toConst(), -1);
|
||||
r.normalize(r.len);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2191,6 +2191,15 @@ test "shift-right negative" {
|
|||
a.setSign(true);
|
||||
try a.shiftRight(&arg7, 4);
|
||||
try testing.expect(try a.toInt(i16) == -2048);
|
||||
|
||||
var arg8_limbs: [1]Limb = undefined;
|
||||
var arg8: Mutable = .{
|
||||
.limbs = &arg8_limbs,
|
||||
.len = undefined,
|
||||
.positive = undefined,
|
||||
};
|
||||
arg8.shiftRight(.{ .limbs = &.{ 1, 1 }, .positive = false }, @bitSizeOf(Limb));
|
||||
try testing.expect(arg8.toConst().orderAgainstScalar(-2).compare(.eq));
|
||||
}
|
||||
|
||||
test "sat shift-left simple unsigned" {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue