]> git.lizzy.rs Git - rust.git/commitdiff
core: Fix overflow in `int::mod_euc` when `self < 0 && rhs == MIN`
authorDaiki Mizukami <mizukami1113@gmail.com>
Mon, 23 Apr 2018 16:45:44 +0000 (01:45 +0900)
committerDaiki Mizukami <mizukami1113@gmail.com>
Mon, 23 Apr 2018 16:53:40 +0000 (01:53 +0900)
src/libcore/num/mod.rs
src/libcore/tests/lib.rs
src/libcore/tests/num/int_macros.rs

index aa4d4bc638b1d236d9f3752a2020e999aaa41684..4893e05badcb541ecd8401bd575c5fe09b730f41 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.is_negative() {
+                        r - rhs
+                    } else {
+                        r + rhs
+                    }
                 } else {
                     r
                 }
index 7d3852d2f2a983c1d1f434a6896078536d1ce42e..ccf647c358d67dabfaf551504d52f8189cd6e057 100644 (file)
@@ -15,6 +15,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);