]> git.lizzy.rs Git - rust.git/commitdiff
test: Fix an overflow on empty benchmarks
authorAlex Crichton <alex@alexcrichton.com>
Fri, 6 Mar 2015 19:58:32 +0000 (11:58 -0800)
committerAlex Crichton <alex@alexcrichton.com>
Fri, 6 Mar 2015 20:01:23 +0000 (12:01 -0800)
Right now the rust upgrade in cargo is blocked on fixing this overflow. If a
this benchmark is run it will trigger an overflow error today:

    #[bench]
    fn foo(b: &mut test::Bencher) {}

This commit adds a check on each iteration of the loop that the maximum
multiplier (10) doesn't overflow, and if it does just return the results so far.

src/libtest/lib.rs

index a855d80f42a4092951af950b7e9eb2d4da8d3b9c..9d1640d10b28cecf2cbdcf853716dc1e4e93ea6b 100644 (file)
@@ -1108,7 +1108,14 @@ pub fn auto_bench<F>(&mut self, mut f: F) -> stats::Summary<f64> where F: FnMut(
                 return summ5;
             }
 
-            n *= 2;
+            // If we overflow here just return the results so far. We check a
+            // multiplier of 10 because we're about to multiply by 2 and the
+            // next iteration of the loop will also multiply by 5 (to calculate
+            // the summ5 result)
+            n = match n.checked_mul(10) {
+                Some(_) => n * 2,
+                None => return summ5,
+            };
         }
     }
 }