Chromium Code Reviews| Index: base/numerics/safe_conversions_impl.h |
| diff --git a/base/numerics/safe_conversions_impl.h b/base/numerics/safe_conversions_impl.h |
| index 41570671601bbc5264bed1990e905ecd9bc46f45..33263157f7f1414fedbcd0aaa41aec03925fa7d6 100644 |
| --- a/base/numerics/safe_conversions_impl.h |
| +++ b/base/numerics/safe_conversions_impl.h |
| @@ -108,6 +108,45 @@ inline RangeConstraint GetRangeConstraint(bool is_in_upper_bound, |
| (is_in_lower_bound ? 0 : RANGE_UNDERFLOW)); |
| } |
| +// The following helper template addresses a corner case in range checks for |
| +// conversion from a floating-point type to an integral type of smaller range |
| +// but larger precision (e.g. float -> int32_t). The problem is as follows: |
| +// 1. Integral maximum is always one less than a power of two, so it must be |
| +// truncated to fit the mantissa of the floating point. The direction of |
| +// rounding is implementation defined, but in practice it's always IEEE |
| +// floats, which round to nearest and thus result in a value of larger |
|
brucedawson
2015/09/11 23:23:27
The FPU can be set to various rounding modes, some
jschuh
2015/09/12 00:43:12
Yes, this should be reliable and completely portab
|
| +// magnitude than the integral value (maximum + 1). |
| +// 2. If the floating point value is equal to the promoted integral maximum |
| +// value, a range check will erroneously pass. |
| +// 3. When the floating point value is then converted to an integer, the |
| +// resulting value is out of range for the target integral type and |
| +// thus is implementation defined. |
| +// To fix this bug we manually truncate the maximum value when the destination |
| +// type is an integral of larger precision than a source floating-point type. |
|
brucedawson
2015/09/11 23:23:27
Could clarify what it is truncated to, perhaps wit
jschuh
2015/09/12 00:43:12
Done.
|
| +template <typename Dst, typename Src> |
| +struct NarrowingRange { |
| + typedef typename std::numeric_limits<Src> SrcLimits; |
| + typedef typename std::numeric_limits<Dst> DstLimits; |
| + |
| + static Dst max() { |
| + // The following logic avoids warnings where the max function is |
| + // instantiated with invalid values for a bit shift (even though |
| + // the such a function can never be called). |
| + static const int shift = |
| + (MaxExponent<Src>::value > MaxExponent<Dst>::value && |
| + SrcLimits::digits < DstLimits::digits && SrcLimits::is_iec559 && |
| + !DstLimits::is_iec559) |
| + ? (DstLimits::digits - SrcLimits::digits) |
| + : 0; |
| + return DstLimits::max() - static_cast<Dst>((UINTMAX_C(1) << shift) - 1); |
| + } |
| + |
| + static Dst min() { |
| + return std::numeric_limits<Dst>::is_iec559 ? -DstLimits::max() |
| + : DstLimits::min(); |
| + } |
| +}; |
| + |
| template < |
| typename Dst, |
| typename Src, |
| @@ -147,11 +186,8 @@ struct DstRangeRelationToSrcRangeImpl<Dst, |
| INTEGER_REPRESENTATION_SIGNED, |
| NUMERIC_RANGE_NOT_CONTAINED> { |
| static RangeConstraint Check(Src value) { |
| - return std::numeric_limits<Dst>::is_iec559 |
| - ? GetRangeConstraint((value < std::numeric_limits<Dst>::max()), |
| - (value > -std::numeric_limits<Dst>::max())) |
| - : GetRangeConstraint((value < std::numeric_limits<Dst>::max()), |
| - (value > std::numeric_limits<Dst>::min())); |
| + return GetRangeConstraint((value <= NarrowingRange<Dst, Src>::max()), |
| + (value >= NarrowingRange<Dst, Src>::min())); |
| } |
| }; |
| @@ -163,7 +199,7 @@ struct DstRangeRelationToSrcRangeImpl<Dst, |
| INTEGER_REPRESENTATION_UNSIGNED, |
| NUMERIC_RANGE_NOT_CONTAINED> { |
| static RangeConstraint Check(Src value) { |
| - return GetRangeConstraint(value < std::numeric_limits<Dst>::max(), true); |
| + return GetRangeConstraint(value <= NarrowingRange<Dst, Src>::max(), true); |
| } |
| }; |
| @@ -178,7 +214,7 @@ struct DstRangeRelationToSrcRangeImpl<Dst, |
| return sizeof(Dst) > sizeof(Src) |
| ? RANGE_VALID |
| : GetRangeConstraint( |
| - value < static_cast<Src>(std::numeric_limits<Dst>::max()), |
| + value <= static_cast<Src>(NarrowingRange<Dst, Src>::max()), |
| true); |
| } |
| }; |
| @@ -195,7 +231,7 @@ struct DstRangeRelationToSrcRangeImpl<Dst, |
| return (MaxExponent<Dst>::value >= MaxExponent<Src>::value) |
| ? GetRangeConstraint(true, value >= static_cast<Src>(0)) |
| : GetRangeConstraint( |
| - value < static_cast<Src>(std::numeric_limits<Dst>::max()), |
| + value <= static_cast<Src>(NarrowingRange<Dst, Src>::max()), |
| value >= static_cast<Src>(0)); |
| } |
| }; |