Rollup merge of #50185 - dmizuk:mod_euc-fix-overflow, r=kennytm
core: Fix overflow in `int::mod_euc` when `self < 0 && rhs == MIN` This commit removes usage of `abs`, which overflows when `self == MIN`.
This commit is contained in:
commit
893774e119
3 changed files with 11 additions and 1 deletions
|
@ -1765,7 +1765,11 @@ assert_eq!((-a).mod_euc(-b), 1);
|
||||||
pub fn mod_euc(self, rhs: Self) -> Self {
|
pub fn mod_euc(self, rhs: Self) -> Self {
|
||||||
let r = self % rhs;
|
let r = self % rhs;
|
||||||
if r < 0 {
|
if r < 0 {
|
||||||
r + rhs.abs()
|
if rhs < 0 {
|
||||||
|
r - rhs
|
||||||
|
} else {
|
||||||
|
r + rhs
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
r
|
r
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
#![feature(core_private_diy_float)]
|
#![feature(core_private_diy_float)]
|
||||||
#![feature(dec2flt)]
|
#![feature(dec2flt)]
|
||||||
#![feature(decode_utf8)]
|
#![feature(decode_utf8)]
|
||||||
|
#![feature(euclidean_division)]
|
||||||
#![feature(exact_size_is_empty)]
|
#![feature(exact_size_is_empty)]
|
||||||
#![feature(fixed_size_array)]
|
#![feature(fixed_size_array)]
|
||||||
#![feature(float_internals)]
|
#![feature(float_internals)]
|
||||||
|
|
|
@ -30,6 +30,11 @@ mod tests {
|
||||||
num::test_num(10 as $T, 2 as $T);
|
num::test_num(10 as $T, 2 as $T);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_mod_euc() {
|
||||||
|
assert!((-1 as $T).mod_euc(MIN) == MAX);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_abs() {
|
pub fn test_abs() {
|
||||||
assert!((1 as $T).abs() == 1 as $T);
|
assert!((1 as $T).abs() == 1 as $T);
|
||||||
|
|
Loading…
Reference in a new issue