OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 * |
| 10 */ |
| 11 |
| 12 // Borrowed from Chromium's src/base/numerics/safe_conversions_impl.h. |
| 13 // - Modified to work in WebRTC (paths, #ifndef, namespace, webrtc/base/checks, |
| 14 // compiler warnings and cpplint.py, etc). |
| 15 // Based on 'chromium_revision': 'ee311243eae6aef9c907543663754ff38f1f4f40'. |
| 16 |
| 17 #ifndef WEBRTC_BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ |
| 18 #define WEBRTC_BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ |
| 19 |
| 20 #include <stdint.h> |
| 21 |
| 22 #include <limits> |
| 23 |
| 24 #include "webrtc/base/checks.h" |
| 25 #include "webrtc/base/logging.h" |
| 26 |
| 27 namespace rtc { |
| 28 namespace internal { |
| 29 |
| 30 // The std library doesn't provide a binary max_exponent for integers, however |
| 31 // we can compute one by adding one to the number of non-sign bits. This allows |
| 32 // for accurate range comparisons between floating point and integer types. |
| 33 template <typename NumericType> |
| 34 struct MaxExponent { |
| 35 static const int value = std::numeric_limits<NumericType>::is_iec559 |
| 36 ? std::numeric_limits<NumericType>::max_exponent |
| 37 : (sizeof(NumericType) * 8 + 1 - |
| 38 std::numeric_limits<NumericType>::is_signed); |
| 39 }; |
| 40 |
| 41 enum IntegerRepresentation { |
| 42 INTEGER_REPRESENTATION_UNSIGNED, |
| 43 INTEGER_REPRESENTATION_SIGNED |
| 44 }; |
| 45 |
| 46 // A range for a given nunmeric Src type is contained for a given numeric Dst |
| 47 // type if both numeric_limits<Src>::max() <= numeric_limits<Dst>::max() and |
| 48 // numeric_limits<Src>::min() >= numeric_limits<Dst>::min() are true. |
| 49 // We implement this as template specializations rather than simple static |
| 50 // comparisons to ensure type correctness in our comparisons. |
| 51 enum NumericRangeRepresentation { |
| 52 NUMERIC_RANGE_NOT_CONTAINED, |
| 53 NUMERIC_RANGE_CONTAINED |
| 54 }; |
| 55 |
| 56 // Helper templates to statically determine if our destination type can contain |
| 57 // maximum and minimum values represented by the source type. |
| 58 |
| 59 template < |
| 60 typename Dst, |
| 61 typename Src, |
| 62 IntegerRepresentation DstSign = std::numeric_limits<Dst>::is_signed |
| 63 ? INTEGER_REPRESENTATION_SIGNED |
| 64 : INTEGER_REPRESENTATION_UNSIGNED, |
| 65 IntegerRepresentation SrcSign = |
| 66 std::numeric_limits<Src>::is_signed |
| 67 ? INTEGER_REPRESENTATION_SIGNED |
| 68 : INTEGER_REPRESENTATION_UNSIGNED > |
| 69 struct StaticDstRangeRelationToSrcRange; |
| 70 |
| 71 // Same sign: Dst is guaranteed to contain Src only if its range is equal or |
| 72 // larger. |
| 73 template <typename Dst, typename Src, IntegerRepresentation Sign> |
| 74 struct StaticDstRangeRelationToSrcRange<Dst, Src, Sign, Sign> { |
| 75 static const NumericRangeRepresentation value = |
| 76 MaxExponent<Dst>::value >= MaxExponent<Src>::value |
| 77 ? NUMERIC_RANGE_CONTAINED |
| 78 : NUMERIC_RANGE_NOT_CONTAINED; |
| 79 }; |
| 80 |
| 81 // Unsigned to signed: Dst is guaranteed to contain source only if its range is |
| 82 // larger. |
| 83 template <typename Dst, typename Src> |
| 84 struct StaticDstRangeRelationToSrcRange<Dst, |
| 85 Src, |
| 86 INTEGER_REPRESENTATION_SIGNED, |
| 87 INTEGER_REPRESENTATION_UNSIGNED> { |
| 88 static const NumericRangeRepresentation value = |
| 89 MaxExponent<Dst>::value > MaxExponent<Src>::value |
| 90 ? NUMERIC_RANGE_CONTAINED |
| 91 : NUMERIC_RANGE_NOT_CONTAINED; |
| 92 }; |
| 93 |
| 94 // Signed to unsigned: Dst cannot be statically determined to contain Src. |
| 95 template <typename Dst, typename Src> |
| 96 struct StaticDstRangeRelationToSrcRange<Dst, |
| 97 Src, |
| 98 INTEGER_REPRESENTATION_UNSIGNED, |
| 99 INTEGER_REPRESENTATION_SIGNED> { |
| 100 static const NumericRangeRepresentation value = NUMERIC_RANGE_NOT_CONTAINED; |
| 101 }; |
| 102 |
| 103 enum RangeConstraint { |
| 104 RANGE_VALID = 0x0, // Value can be represented by the destination type. |
| 105 RANGE_UNDERFLOW = 0x1, // Value would overflow. |
| 106 RANGE_OVERFLOW = 0x2, // Value would underflow. |
| 107 RANGE_INVALID = RANGE_UNDERFLOW | RANGE_OVERFLOW // Invalid (i.e. NaN). |
| 108 }; |
| 109 |
| 110 // Helper function for coercing an int back to a RangeContraint. |
| 111 inline RangeConstraint GetRangeConstraint(int integer_range_constraint) { |
| 112 RTC_DCHECK(integer_range_constraint >= RANGE_VALID && |
| 113 integer_range_constraint <= RANGE_INVALID); |
| 114 return static_cast<RangeConstraint>(integer_range_constraint); |
| 115 } |
| 116 |
| 117 // This function creates a RangeConstraint from an upper and lower bound |
| 118 // check by taking advantage of the fact that only NaN can be out of range in |
| 119 // both directions at once. |
| 120 inline RangeConstraint GetRangeConstraint(bool is_in_upper_bound, |
| 121 bool is_in_lower_bound) { |
| 122 return GetRangeConstraint((is_in_upper_bound ? 0 : RANGE_OVERFLOW) | |
| 123 (is_in_lower_bound ? 0 : RANGE_UNDERFLOW)); |
| 124 } |
| 125 |
| 126 // The following helper template addresses a corner case in range checks for |
| 127 // conversion from a floating-point type to an integral type of smaller range |
| 128 // but larger precision (e.g. float -> unsigned). The problem is as follows: |
| 129 // 1. Integral maximum is always one less than a power of two, so it must be |
| 130 // truncated to fit the mantissa of the floating point. The direction of |
| 131 // rounding is implementation defined, but by default it's always IEEE |
| 132 // floats, which round to nearest and thus result in a value of larger |
| 133 // magnitude than the integral value. |
| 134 // Example: float f = UINT_MAX; // f is 4294967296f but UINT_MAX |
| 135 // // is 4294967295u. |
| 136 // 2. If the floating point value is equal to the promoted integral maximum |
| 137 // value, a range check will erroneously pass. |
| 138 // Example: (4294967296f <= 4294967295u) // This is true due to a precision |
| 139 // // loss in rounding up to float. |
| 140 // 3. When the floating point value is then converted to an integral, the |
| 141 // resulting value is out of range for the target integral type and |
| 142 // thus is implementation defined. |
| 143 // Example: unsigned u = (float)INT_MAX; // u will typically overflow to 0. |
| 144 // To fix this bug we manually truncate the maximum value when the destination |
| 145 // type is an integral of larger precision than the source floating-point type, |
| 146 // such that the resulting maximum is represented exactly as a floating point. |
| 147 template <typename Dst, typename Src> |
| 148 struct NarrowingRange { |
| 149 typedef typename std::numeric_limits<Src> SrcLimits; |
| 150 typedef typename std::numeric_limits<Dst> DstLimits; |
| 151 |
| 152 static Dst max() { |
| 153 // The following logic avoids warnings where the max function is |
| 154 // instantiated with invalid values for a bit shift (even though |
| 155 // such a function can never be called). |
| 156 static const int shift = |
| 157 (MaxExponent<Src>::value > MaxExponent<Dst>::value && |
| 158 SrcLimits::digits < DstLimits::digits && SrcLimits::is_iec559 && |
| 159 DstLimits::is_integer) |
| 160 ? (DstLimits::digits - SrcLimits::digits) |
| 161 : 0; |
| 162 |
| 163 // We use UINTMAX_C below to avoid compiler warnings about shifting floating |
| 164 // points. Since it's a compile time calculation, it shouldn't have any |
| 165 // performance impact. |
| 166 return DstLimits::max() - static_cast<Dst>((UINTMAX_C(1) << shift) - 1); |
| 167 } |
| 168 |
| 169 static Dst min() { |
| 170 return std::numeric_limits<Dst>::is_iec559 ? -DstLimits::max() |
| 171 : DstLimits::min(); |
| 172 } |
| 173 }; |
| 174 |
| 175 template < |
| 176 typename Dst, |
| 177 typename Src, |
| 178 IntegerRepresentation DstSign = std::numeric_limits<Dst>::is_signed |
| 179 ? INTEGER_REPRESENTATION_SIGNED |
| 180 : INTEGER_REPRESENTATION_UNSIGNED, |
| 181 IntegerRepresentation SrcSign = std::numeric_limits<Src>::is_signed |
| 182 ? INTEGER_REPRESENTATION_SIGNED |
| 183 : INTEGER_REPRESENTATION_UNSIGNED, |
| 184 NumericRangeRepresentation DstRange = |
| 185 StaticDstRangeRelationToSrcRange<Dst, Src>::value > |
| 186 struct DstRangeRelationToSrcRangeImpl; |
| 187 |
| 188 // The following templates are for ranges that must be verified at runtime. We |
| 189 // split it into checks based on signedness to avoid confusing casts and |
| 190 // compiler warnings on signed an unsigned comparisons. |
| 191 |
| 192 // Dst range is statically determined to contain Src: Nothing to check. |
| 193 template <typename Dst, |
| 194 typename Src, |
| 195 IntegerRepresentation DstSign, |
| 196 IntegerRepresentation SrcSign> |
| 197 struct DstRangeRelationToSrcRangeImpl<Dst, |
| 198 Src, |
| 199 DstSign, |
| 200 SrcSign, |
| 201 NUMERIC_RANGE_CONTAINED> { |
| 202 static RangeConstraint Check(Src value) { return RANGE_VALID; } |
| 203 }; |
| 204 |
| 205 // Signed to signed narrowing: Both the upper and lower boundaries may be |
| 206 // exceeded. |
| 207 template <typename Dst, typename Src> |
| 208 struct DstRangeRelationToSrcRangeImpl<Dst, |
| 209 Src, |
| 210 INTEGER_REPRESENTATION_SIGNED, |
| 211 INTEGER_REPRESENTATION_SIGNED, |
| 212 NUMERIC_RANGE_NOT_CONTAINED> { |
| 213 static RangeConstraint Check(Src value) { |
| 214 return GetRangeConstraint((value <= NarrowingRange<Dst, Src>::max()), |
| 215 (value >= NarrowingRange<Dst, Src>::min())); |
| 216 } |
| 217 }; |
| 218 |
| 219 // Unsigned to unsigned narrowing: Only the upper boundary can be exceeded. |
| 220 template <typename Dst, typename Src> |
| 221 struct DstRangeRelationToSrcRangeImpl<Dst, |
| 222 Src, |
| 223 INTEGER_REPRESENTATION_UNSIGNED, |
| 224 INTEGER_REPRESENTATION_UNSIGNED, |
| 225 NUMERIC_RANGE_NOT_CONTAINED> { |
| 226 static RangeConstraint Check(Src value) { |
| 227 return GetRangeConstraint(value <= NarrowingRange<Dst, Src>::max(), true); |
| 228 } |
| 229 }; |
| 230 |
| 231 // Unsigned to signed: The upper boundary may be exceeded. |
| 232 template <typename Dst, typename Src> |
| 233 struct DstRangeRelationToSrcRangeImpl<Dst, |
| 234 Src, |
| 235 INTEGER_REPRESENTATION_SIGNED, |
| 236 INTEGER_REPRESENTATION_UNSIGNED, |
| 237 NUMERIC_RANGE_NOT_CONTAINED> { |
| 238 static RangeConstraint Check(Src value) { |
| 239 return sizeof(Dst) > sizeof(Src) |
| 240 ? RANGE_VALID |
| 241 : GetRangeConstraint( |
| 242 value <= static_cast<Src>(NarrowingRange<Dst, Src>::max()), |
| 243 true); |
| 244 } |
| 245 }; |
| 246 |
| 247 // Signed to unsigned: The upper boundary may be exceeded for a narrower Dst, |
| 248 // and any negative value exceeds the lower boundary. |
| 249 template <typename Dst, typename Src> |
| 250 struct DstRangeRelationToSrcRangeImpl<Dst, |
| 251 Src, |
| 252 INTEGER_REPRESENTATION_UNSIGNED, |
| 253 INTEGER_REPRESENTATION_SIGNED, |
| 254 NUMERIC_RANGE_NOT_CONTAINED> { |
| 255 static RangeConstraint Check(Src value) { |
| 256 return (MaxExponent<Dst>::value >= MaxExponent<Src>::value) |
| 257 ? GetRangeConstraint(true, value >= static_cast<Src>(0)) |
| 258 : GetRangeConstraint( |
| 259 value <= static_cast<Src>(NarrowingRange<Dst, Src>::max()), |
| 260 value >= static_cast<Src>(0)); |
| 261 } |
| 262 }; |
| 263 |
| 264 template <typename Dst, typename Src> |
| 265 inline RangeConstraint DstRangeRelationToSrcRange(Src value) { |
| 266 static_assert(std::numeric_limits<Src>::is_specialized, |
| 267 "Argument must be numeric."); |
| 268 static_assert(std::numeric_limits<Dst>::is_specialized, |
| 269 "Result must be numeric."); |
| 270 return DstRangeRelationToSrcRangeImpl<Dst, Src>::Check(value); |
| 271 } |
| 272 |
| 273 } // namespace internal |
| 274 } // namespace rtc |
| 275 |
| 276 #endif // WEBRTC_BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ |
OLD | NEW |