Chromium Code Reviews| Index: base/numerics/safe_math_impl.h |
| diff --git a/base/numerics/safe_math_impl.h b/base/numerics/safe_math_impl.h |
| index 94ae89494d428a294a3729e6c307669bc3b98cb2..cfc7e38993acac89920b316fe683ad5fefe87909 100644 |
| --- a/base/numerics/safe_math_impl.h |
| +++ b/base/numerics/safe_math_impl.h |
| @@ -205,24 +205,24 @@ CheckedMul(T x, T y, RangeConstraint* validity) { |
| if (!x || !y) { |
| *validity = RANGE_VALID; |
| return static_cast<T>(0); |
| - |
| - } else if (x > 0) { |
| - if (y > 0) |
| + } |
| + if (x > 0) { |
| + if (y > 0) { |
| *validity = |
| x <= std::numeric_limits<T>::max() / y ? RANGE_VALID : RANGE_OVERFLOW; |
| - else |
| + } else { |
| *validity = y >= std::numeric_limits<T>::min() / x ? RANGE_VALID |
| : RANGE_UNDERFLOW; |
| - |
| + } |
| } else { |
| - if (y > 0) |
| + if (y > 0) { |
| *validity = x >= std::numeric_limits<T>::min() / y ? RANGE_VALID |
| : RANGE_UNDERFLOW; |
| - else |
| + } else { |
| *validity = |
| y >= std::numeric_limits<T>::max() / x ? RANGE_VALID : RANGE_OVERFLOW; |
| + } |
| } |
|
jschuh
2016/11/01 17:21:55
Could you move this out to another CL, since it's
|
| - |
| return static_cast<T>(*validity == RANGE_VALID ? x * y : 0); |
| } |
| @@ -238,13 +238,18 @@ CheckedMul(T x, T y, RangeConstraint* validity) { |
| return static_cast<T>(*validity == RANGE_VALID ? x * y : 0); |
| } |
| -// Division just requires a check for an invalid negation on signed min/-1. |
| +// Division just requires a check for a zero denominator or an invalid negation |
| +// on signed min/-1. |
| template <typename T> |
| T CheckedDiv(T x, |
| T y, |
| RangeConstraint* validity, |
| typename std::enable_if<std::numeric_limits<T>::is_integer, |
| int>::type = 0) { |
| + if (y == 0) { |
| + *validity = RANGE_INVALID; |
| + return static_cast<T>(0); |
| + } |
| if (std::numeric_limits<T>::is_signed && x == std::numeric_limits<T>::min() && |
| y == static_cast<T>(-1)) { |
| *validity = RANGE_OVERFLOW; |
| @@ -269,8 +274,8 @@ typename std::enable_if<std::numeric_limits<T>::is_integer && |
| !std::numeric_limits<T>::is_signed, |
| T>::type |
| CheckedMod(T x, T y, RangeConstraint* validity) { |
| - *validity = RANGE_VALID; |
| - return static_cast<T>(x % y); |
| + *validity = y != 0 ? RANGE_VALID : RANGE_INVALID; |
| + return static_cast<T>(*validity == RANGE_VALID ? x % y: 0); |
| } |
| template <typename T> |