]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #50185 - dmizuk:mod_euc-fix-overflow, r=kennytm
authorkennytm <kennytm@gmail.com>
Tue, 24 Apr 2018 03:57:11 +0000 (11:57 +0800)
committerGitHub <noreply@github.com>
Tue, 24 Apr 2018 03:57:11 +0000 (11:57 +0800)
core: Fix overflow in `int::mod_euc` when `self < 0 && rhs == MIN`

This commit removes usage of `abs`, which overflows when `self == MIN`.

src/libcore/num/mod.rs
src/libcore/tests/lib.rs
src/libcore/tests/num/int_macros.rs

index aa4d4bc638b1d236d9f3752a2020e999aaa41684..a062fbda5bad0bcd0add87188743a596f2679a5b 100644 (file)
@@ -1765,7 +1765,11 @@ pub fn div_euc(self, rhs: Self) -> Self {
             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
                 }
index 2b69f04013d201a320a927648a011a4319baa0e2..e4d277179382f3e522259bbc67abebafaab8b3d4 100644 (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)]
index 8d791283ab87ebd22846667445f7414dc53a60b9..71d2e7945389ba63aac87a3844a1d55344821d1f 100644 (file)
@@ -30,6 +30,11 @@ fn test_num() {
         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);