Эх сурвалжийг харах

Made it panic on gcd = min_val in debug mode

The additional call to .abs() causes a panic on the min value. There are
no other negative values it can encounter.
Does nothing in release mode
Emerentius 9 жил өмнө
parent
commit
59089d9d5c
1 өөрчлөгдсөн 11 нэмэгдсэн , 8 устгасан
  1. 11 8
      src/integer.rs

+ 11 - 8
src/integer.rs

@@ -225,14 +225,17 @@ macro_rules! impl_integer_for_isize {
                 // find common factors of 2
                 let shift = (m | n).trailing_zeros();
 
-                // If one number is the minimum value, it cannot be represented as a
-                // positive number. It's also a power of two, so the gcd can
-                // trivially be calculated in that case by bitshifting
-
-                // The result is always positive in two's complement, unless
-                // n and m are the minimum value, then it's negative
-                // no other way to represent that number
-                if m == <$T>::min_value() || n == <$T>::min_value() { return 1 << shift }
+                // The algorithm needs positive numbers, but the minimum value
+                // can't be represented as a positive one.
+                // It's also a power of two, so the gcd can be
+                // calculated by bitshifting in that case
+
+                // Assuming two's complement, the number created by the shift
+                // is positive for all numbers except gcd = abs(min value)
+                // The call to .abs() causes a panic in debug mode
+                if m == <$T>::min_value() || n == <$T>::min_value() {
+                    return (1 << shift).abs()
+                }
 
                 // guaranteed to be positive now, rest like unsigned algorithm
                 m = m.abs();