core: Fix overflow in int::mod_euc when self < 0 && rhs == MIN

This commit is contained in:
Daiki Mizukami
2018-04-24 01:45:44 +09:00
parent 1fac3ca9c4
commit fbb1c280bf
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.is_negative() {
r - rhs
} else {
r + rhs
}
} else {
r
}