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:
kennytm 2018-04-24 11:57:11 +08:00 committed by GitHub
commit 893774e119
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 1 deletions

View file

@ -1765,7 +1765,11 @@ assert_eq!((-a).mod_euc(-b), 1);
pub fn mod_euc(self, rhs: Self) -> Self {
let r = self % rhs;
if r < 0 {
r + rhs.abs()
if rhs < 0 {
r - rhs
} else {
r + rhs
}
} else {
r
}

View file

@ -16,6 +16,7 @@
#![feature(core_private_diy_float)]
#![feature(dec2flt)]
#![feature(decode_utf8)]
#![feature(euclidean_division)]
#![feature(exact_size_is_empty)]
#![feature(fixed_size_array)]
#![feature(float_internals)]

View file

@ -30,6 +30,11 @@ mod tests {
num::test_num(10 as $T, 2 as $T);
}
#[test]
fn test_mod_euc() {
assert!((-1 as $T).mod_euc(MIN) == MAX);
}
#[test]
pub fn test_abs() {
assert!((1 as $T).abs() == 1 as $T);