Chromium Code Reviews
|
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef BASE_SAFE_NUMERICS_IMPL_H_ | |
| 6 #define BASE_SAFE_NUMERICS_IMPL_H_ | |
| 7 | |
| 8 #include <limits> | |
| 9 | |
| 10 #include "base/macros.h" | |
| 11 | |
| 12 namespace base { | |
| 13 namespace internal { | |
| 14 | |
| 15 // Helper templates to determine if our destination type can contain all values | |
| 16 // represented by the source type. | |
| 17 | |
| 18 template<typename Dest, | |
|
akalin
2014/01/14 00:49:19
nit: space before <
jschuh
2014/01/14 05:24:33
Done.
| |
| 19 typename Source, | |
| 20 bool IsDestSigned = std::numeric_limits<Dest>::is_signed, | |
|
akalin
2014/01/14 00:49:19
perhaps define enums for DestSigned and SourceSign
jschuh
2014/01/14 05:24:33
Done.
| |
| 21 bool IsSourceSigned = std::numeric_limits<Source>::is_signed> | |
| 22 struct DoesRangeContainRange {}; | |
| 23 | |
| 24 template<typename Dest, typename Source, bool IsSigned> | |
|
akalin
2014/01/14 00:49:19
doesn't look like this overload is used. remove?
jschuh
2014/01/14 05:24:33
It caught the case where the signs matched, but I
| |
| 25 struct DoesRangeContainRange<Dest, Source, IsSigned, IsSigned> { | |
| 26 // We always treat float to integral as narrowing. | |
| 27 static const bool value = std::numeric_limits<Dest>::is_integer && | |
|
akalin
2014/01/14 00:49:19
maybe use an enum for this, too? e.g. CONTAINS_RAN
jschuh
2014/01/14 05:24:33
Done.
| |
| 28 sizeof(Dest) >= sizeof(Source); | |
| 29 }; | |
| 30 | |
| 31 template<typename Dest, typename Source> | |
| 32 struct DoesRangeContainRange<Dest, Source, true, false> { | |
| 33 // We always treat float to integral as narrowing. | |
| 34 static const bool value = std::numeric_limits<Dest>::is_integer && | |
| 35 sizeof(Dest) > sizeof(Source); | |
| 36 }; | |
| 37 | |
| 38 template<typename Dest, typename Source> | |
| 39 struct DoesRangeContainRange<Dest, Source, false, true> { | |
| 40 static const bool value = false; | |
| 41 }; | |
| 42 | |
| 43 | |
| 44 // The following templates are for ranges that must be verified at runtime. We | |
| 45 // split it into checks based on signedness to avoid confusing casts and | |
| 46 // compiler warnings on signed an unsigned comparisons. | |
| 47 | |
| 48 enum NumericRangeCheckResult { | |
| 49 TYPE_VALID = 0, // Value can be represented by the destination type. | |
| 50 TYPE_UNDERFLOW = 1, // Value would overflow. | |
| 51 TYPE_OVERFLOW = 2, // Value would underflow. | |
| 52 TYPE_INVALID = 3 // Source value is invalid (i.e. NaN). | |
| 53 }; | |
| 54 | |
| 55 // This macro creates a NumericRangeCheckResult from an upper and lower bound | |
| 56 // check by taking advantage of the fact that only NaN can be out of range in | |
| 57 // both directions at once. | |
| 58 #define BASE_NUMERIC_RANGE_CHECK_RESULT(is_in_upper_bound, is_in_lower_bound) \ | |
| 59 NumericRangeCheckResult(((is_in_upper_bound) ? 0 : TYPE_OVERFLOW) | \ | |
| 60 ((is_in_lower_bound) ? 0 : TYPE_UNDERFLOW)) | |
| 61 | |
| 62 template<typename Dest, | |
| 63 typename Source, | |
| 64 bool IsDestSigned = std::numeric_limits<Dest>::is_signed, | |
| 65 bool IsSourceSigned = std::numeric_limits<Source>::is_signed, | |
| 66 bool DoesDestRangeContainSourceRange = DoesRangeContainRange<Dest, | |
| 67 Source>::value> | |
| 68 struct NumericRangeCheckImpl {}; | |
| 69 | |
| 70 // Dest range always contains the result: nothing to check. | |
| 71 template<typename Dest, typename Source, bool IsDestSigned, bool IsSourceSigned> | |
| 72 struct NumericRangeCheckImpl<Dest, Source, IsDestSigned, IsSourceSigned, true> { | |
| 73 static NumericRangeCheckResult run(Source) { | |
|
akalin
2014/01/14 00:49:19
would name this Check() myself
akalin
2014/01/14 00:49:19
I'd prefer (Source x), even though you don't use x
| |
| 74 return BASE_NUMERIC_RANGE_CHECK_RESULT(true, true); | |
| 75 } | |
| 76 }; | |
| 77 | |
| 78 // Signed to signed narrowing. | |
| 79 template<typename Dest, typename Source> | |
| 80 struct NumericRangeCheckImpl<Dest, Source, true, true, false> { | |
| 81 static NumericRangeCheckResult run(Source x) { | |
| 82 typedef std::numeric_limits<Dest> DestLimits; | |
| 83 return BASE_NUMERIC_RANGE_CHECK_RESULT(x <= DestLimits::max(), | |
| 84 x >= DestLimits::min()); | |
| 85 } | |
| 86 }; | |
| 87 | |
| 88 // Unsigned to unsigned narrowing. | |
| 89 template<typename Dest, typename Source> | |
| 90 struct NumericRangeCheckImpl<Dest, Source, false, false, false> { | |
| 91 static NumericRangeCheckResult run(Source x) { | |
| 92 typedef std::numeric_limits<Dest> DestLimits; | |
| 93 return BASE_NUMERIC_RANGE_CHECK_RESULT(x <= DestLimits::max(), true); | |
| 94 } | |
| 95 }; | |
| 96 | |
| 97 // Unsigned to signed. | |
| 98 template<typename Dest, typename Source> | |
| 99 struct NumericRangeCheckImpl<Dest, Source, true, false, false> { | |
| 100 static NumericRangeCheckResult run(Source x) { | |
| 101 typedef std::numeric_limits<Dest> DestLimits; | |
| 102 return sizeof(Dest) >= sizeof(Source) ? | |
| 103 BASE_NUMERIC_RANGE_CHECK_RESULT(true, true) : | |
| 104 BASE_NUMERIC_RANGE_CHECK_RESULT(x <= Source(DestLimits::max()), true); | |
| 105 } | |
| 106 }; | |
| 107 | |
| 108 // Signed to unsigned. | |
| 109 template<typename Dest, typename Source> | |
| 110 struct NumericRangeCheckImpl<Dest, Source, false, true, false> { | |
| 111 static NumericRangeCheckResult run(Source x) { | |
| 112 typedef std::numeric_limits<Dest> DestLimits; | |
| 113 return sizeof(Dest) >= sizeof(Source) ? | |
| 114 BASE_NUMERIC_RANGE_CHECK_RESULT(true, x >= 0) : | |
| 115 BASE_NUMERIC_RANGE_CHECK_RESULT(x <= Source(DestLimits::max()), x >= 0); | |
| 116 } | |
| 117 }; | |
| 118 | |
| 119 template<typename Dest, typename Source> | |
| 120 inline NumericRangeCheckResult NumericRangeCheck(Source x) { | |
| 121 COMPILE_ASSERT(std::numeric_limits<Source>::is_specialized, | |
| 122 argument_must_be_numeric); | |
| 123 COMPILE_ASSERT(std::numeric_limits<Dest>::is_specialized, | |
| 124 argument_must_be_numeric); | |
| 125 COMPILE_ASSERT(std::numeric_limits<Dest>::is_integer, | |
| 126 result_must_be_integral); | |
| 127 return NumericRangeCheckImpl<Dest, Source>::run(x); | |
| 128 } | |
| 129 | |
| 130 } // namespace internal | |
| 131 } // namespace base | |
| 132 | |
| 133 #endif // BASE_SAFE_NUMERICS_IMPL_H_ | |
| 134 | |
| OLD | NEW |