libstd: modify wrong shift width.
borrow = *elem << (uint::bits - n_bits); The code above contains a bug that the value of the right operand of the shift operator exceeds the size of the left operand, because sizeof(*elem) == 32, and 0 <= n_bits < 32 in 64bit architecture. If `--opt-level` option is not given to rustc, the code above runs as if the right operand is `(uint::bits - n_bits) % 32`, but if --opt-level is given, `borrow` is always zero. I wonder why this bug is not catched in the libstd's testsuite (I try the `rustc --test --opt-level=2 bigint.rs` before fixing the bug, but the unittest passes normally.)
This commit is contained in:
parent
e4ca2da420
commit
ffa31d235b
@ -623,7 +623,7 @@ impl BigUint {
|
||||
let mut shifted = ~[];
|
||||
for self.data.each_reverse |elem| {
|
||||
shifted = ~[(*elem >> n_bits) | borrow] + shifted;
|
||||
borrow = *elem << (uint::bits - n_bits);
|
||||
borrow = *elem << (BigDigit::bits - n_bits);
|
||||
}
|
||||
return BigUint::new(shifted);
|
||||
}
|
||||
@ -1213,6 +1213,7 @@ mod biguint_tests {
|
||||
check(~[1 << 2], 2, ~[1]);
|
||||
check(~[1, 2], 3, ~[1 << (BigDigit::bits - 2)]);
|
||||
check(~[1, 1, 2], 3 + BigDigit::bits, ~[1 << (BigDigit::bits - 2)]);
|
||||
check(~[0, 1], 1, ~[0x80000000]);
|
||||
test_shr_bits();
|
||||
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
|
Loading…
x
Reference in New Issue
Block a user