]> git.lizzy.rs Git - rust.git/commitdiff
miri: fix exact_div
authorRalf Jung <post@ralfj.de>
Thu, 13 Feb 2020 10:16:54 +0000 (11:16 +0100)
committerRalf Jung <post@ralfj.de>
Thu, 13 Feb 2020 10:21:29 +0000 (11:21 +0100)
src/librustc_mir/interpret/intrinsics.rs

index f85da760ada6d03c5af4262172a761ec422ea0ba..33070f7e65671d3f6c67268ee79441433697f0c4 100644 (file)
@@ -410,8 +410,9 @@ pub fn exact_div(
     ) -> InterpResult<'tcx> {
         // Performs an exact division, resulting in undefined behavior where
         // `x % y != 0` or `y == 0` or `x == T::min_value() && y == -1`.
-        // First, check x % y != 0.
-        if self.binary_op(BinOp::Rem, a, b)?.to_bits()? != 0 {
+        // First, check x % y != 0 (or if that computation overflows).
+        let (res, overflow, _ty) = self.overflowing_binary_op(BinOp::Rem, a, b)?;
+        if overflow || res.to_bits(a.layout.size)? != 0 {
             // Then, check if `b` is -1, which is the "min_value / -1" case.
             let minus1 = Scalar::from_int(-1, dest.layout.size);
             let b_scalar = b.to_scalar().unwrap();
@@ -421,6 +422,7 @@ pub fn exact_div(
                 throw_ub_format!("exact_div: {} cannot be divided by {} without remainder", a, b,)
             }
         }
+        // `Rem` says this is all right, so we can let `Div` do its job.
         self.binop_ignore_overflow(BinOp::Div, a, b, dest)
     }
 }