Tommi | d44c077 | 2016-03-11 17:12:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | // Borrowed from Chromium's src/base/numerics/safe_conversions_impl.h. |
| 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #ifndef RTC_BASE_SAFE_CONVERSIONS_IMPL_H_ |
| 14 | #define RTC_BASE_SAFE_CONVERSIONS_IMPL_H_ |
Tommi | d44c077 | 2016-03-11 17:12:32 -0800 | [diff] [blame] | 15 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 16 | #include <limits> |
Tommi | d44c077 | 2016-03-11 17:12:32 -0800 | [diff] [blame] | 17 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 18 | namespace rtc { |
| 19 | namespace internal { |
| 20 | |
Karl Wiberg | fd0de7a | 2017-11-07 11:02:40 +0100 | [diff] [blame] | 21 | enum DstSign { DST_UNSIGNED, DST_SIGNED }; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 22 | |
Karl Wiberg | fd0de7a | 2017-11-07 11:02:40 +0100 | [diff] [blame] | 23 | enum SrcSign { SRC_UNSIGNED, SRC_SIGNED }; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 24 | |
Karl Wiberg | fd0de7a | 2017-11-07 11:02:40 +0100 | [diff] [blame] | 25 | enum DstRange { OVERLAPS_RANGE, CONTAINS_RANGE }; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 26 | |
| 27 | // Helper templates to statically determine if our destination type can contain |
| 28 | // all values represented by the source type. |
| 29 | |
Karl Wiberg | fd0de7a | 2017-11-07 11:02:40 +0100 | [diff] [blame] | 30 | template <typename Dst, |
| 31 | typename Src, |
| 32 | DstSign IsDstSigned = |
| 33 | std::numeric_limits<Dst>::is_signed ? DST_SIGNED : DST_UNSIGNED, |
| 34 | SrcSign IsSrcSigned = |
| 35 | std::numeric_limits<Src>::is_signed ? SRC_SIGNED : SRC_UNSIGNED> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 36 | struct StaticRangeCheck {}; |
| 37 | |
| 38 | template <typename Dst, typename Src> |
| 39 | struct StaticRangeCheck<Dst, Src, DST_SIGNED, SRC_SIGNED> { |
| 40 | typedef std::numeric_limits<Dst> DstLimits; |
| 41 | typedef std::numeric_limits<Src> SrcLimits; |
| 42 | // Compare based on max_exponent, which we must compute for integrals. |
Karl Wiberg | fd0de7a | 2017-11-07 11:02:40 +0100 | [diff] [blame] | 43 | static const size_t kDstMaxExponent = |
| 44 | DstLimits::is_iec559 ? DstLimits::max_exponent : (sizeof(Dst) * 8 - 1); |
| 45 | static const size_t kSrcMaxExponent = |
| 46 | SrcLimits::is_iec559 ? SrcLimits::max_exponent : (sizeof(Src) * 8 - 1); |
| 47 | static const DstRange value = |
| 48 | kDstMaxExponent >= kSrcMaxExponent ? CONTAINS_RANGE : OVERLAPS_RANGE; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | template <typename Dst, typename Src> |
| 52 | struct StaticRangeCheck<Dst, Src, DST_UNSIGNED, SRC_UNSIGNED> { |
Karl Wiberg | fd0de7a | 2017-11-07 11:02:40 +0100 | [diff] [blame] | 53 | static const DstRange value = |
| 54 | sizeof(Dst) >= sizeof(Src) ? CONTAINS_RANGE : OVERLAPS_RANGE; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | template <typename Dst, typename Src> |
| 58 | struct StaticRangeCheck<Dst, Src, DST_SIGNED, SRC_UNSIGNED> { |
| 59 | typedef std::numeric_limits<Dst> DstLimits; |
| 60 | typedef std::numeric_limits<Src> SrcLimits; |
| 61 | // Compare based on max_exponent, which we must compute for integrals. |
Karl Wiberg | fd0de7a | 2017-11-07 11:02:40 +0100 | [diff] [blame] | 62 | static const size_t kDstMaxExponent = |
| 63 | DstLimits::is_iec559 ? DstLimits::max_exponent : (sizeof(Dst) * 8 - 1); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 64 | static const size_t kSrcMaxExponent = sizeof(Src) * 8; |
Karl Wiberg | fd0de7a | 2017-11-07 11:02:40 +0100 | [diff] [blame] | 65 | static const DstRange value = |
| 66 | kDstMaxExponent >= kSrcMaxExponent ? CONTAINS_RANGE : OVERLAPS_RANGE; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | template <typename Dst, typename Src> |
| 70 | struct StaticRangeCheck<Dst, Src, DST_UNSIGNED, SRC_SIGNED> { |
| 71 | static const DstRange value = OVERLAPS_RANGE; |
| 72 | }; |
| 73 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 74 | enum RangeCheckResult { |
| 75 | TYPE_VALID = 0, // Value can be represented by the destination type. |
| 76 | TYPE_UNDERFLOW = 1, // Value would overflow. |
| 77 | TYPE_OVERFLOW = 2, // Value would underflow. |
| 78 | TYPE_INVALID = 3 // Source value is invalid (i.e. NaN). |
| 79 | }; |
| 80 | |
| 81 | // This macro creates a RangeCheckResult from an upper and lower bound |
| 82 | // check by taking advantage of the fact that only NaN can be out of range in |
| 83 | // both directions at once. |
| 84 | #define BASE_NUMERIC_RANGE_CHECK_RESULT(is_in_upper_bound, is_in_lower_bound) \ |
Karl Wiberg | fd0de7a | 2017-11-07 11:02:40 +0100 | [diff] [blame] | 85 | RangeCheckResult(((is_in_upper_bound) ? 0 : TYPE_OVERFLOW) | \ |
| 86 | ((is_in_lower_bound) ? 0 : TYPE_UNDERFLOW)) |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 87 | |
| 88 | template <typename Dst, |
| 89 | typename Src, |
Karl Wiberg | fd0de7a | 2017-11-07 11:02:40 +0100 | [diff] [blame] | 90 | DstSign IsDstSigned = |
| 91 | std::numeric_limits<Dst>::is_signed ? DST_SIGNED : DST_UNSIGNED, |
| 92 | SrcSign IsSrcSigned = |
| 93 | std::numeric_limits<Src>::is_signed ? SRC_SIGNED : SRC_UNSIGNED, |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 94 | DstRange IsSrcRangeContained = StaticRangeCheck<Dst, Src>::value> |
| 95 | struct RangeCheckImpl {}; |
| 96 | |
| 97 | // The following templates are for ranges that must be verified at runtime. We |
| 98 | // split it into checks based on signedness to avoid confusing casts and |
| 99 | // compiler warnings on signed an unsigned comparisons. |
| 100 | |
| 101 | // Dst range always contains the result: nothing to check. |
| 102 | template <typename Dst, typename Src, DstSign IsDstSigned, SrcSign IsSrcSigned> |
| 103 | struct RangeCheckImpl<Dst, Src, IsDstSigned, IsSrcSigned, CONTAINS_RANGE> { |
Karl Wiberg | fd0de7a | 2017-11-07 11:02:40 +0100 | [diff] [blame] | 104 | static RangeCheckResult Check(Src value) { return TYPE_VALID; } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | // Signed to signed narrowing. |
| 108 | template <typename Dst, typename Src> |
| 109 | struct RangeCheckImpl<Dst, Src, DST_SIGNED, SRC_SIGNED, OVERLAPS_RANGE> { |
| 110 | static RangeCheckResult Check(Src value) { |
| 111 | typedef std::numeric_limits<Dst> DstLimits; |
Karl Wiberg | fd0de7a | 2017-11-07 11:02:40 +0100 | [diff] [blame] | 112 | return DstLimits::is_iec559 |
| 113 | ? BASE_NUMERIC_RANGE_CHECK_RESULT( |
| 114 | value <= static_cast<Src>(DstLimits::max()), |
| 115 | value >= static_cast<Src>(DstLimits::max() * -1)) |
| 116 | : BASE_NUMERIC_RANGE_CHECK_RESULT( |
| 117 | value <= static_cast<Src>(DstLimits::max()), |
| 118 | value >= static_cast<Src>(DstLimits::min())); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 119 | } |
| 120 | }; |
| 121 | |
| 122 | // Unsigned to unsigned narrowing. |
| 123 | template <typename Dst, typename Src> |
| 124 | struct RangeCheckImpl<Dst, Src, DST_UNSIGNED, SRC_UNSIGNED, OVERLAPS_RANGE> { |
| 125 | static RangeCheckResult Check(Src value) { |
| 126 | typedef std::numeric_limits<Dst> DstLimits; |
| 127 | return BASE_NUMERIC_RANGE_CHECK_RESULT( |
Karl Wiberg | fd0de7a | 2017-11-07 11:02:40 +0100 | [diff] [blame] | 128 | value <= static_cast<Src>(DstLimits::max()), true); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 129 | } |
| 130 | }; |
| 131 | |
| 132 | // Unsigned to signed. |
| 133 | template <typename Dst, typename Src> |
| 134 | struct RangeCheckImpl<Dst, Src, DST_SIGNED, SRC_UNSIGNED, OVERLAPS_RANGE> { |
| 135 | static RangeCheckResult Check(Src value) { |
| 136 | typedef std::numeric_limits<Dst> DstLimits; |
Karl Wiberg | fd0de7a | 2017-11-07 11:02:40 +0100 | [diff] [blame] | 137 | return sizeof(Dst) > sizeof(Src) |
| 138 | ? TYPE_VALID |
| 139 | : BASE_NUMERIC_RANGE_CHECK_RESULT( |
| 140 | value <= static_cast<Src>(DstLimits::max()), true); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 141 | } |
| 142 | }; |
| 143 | |
| 144 | // Signed to unsigned. |
| 145 | template <typename Dst, typename Src> |
| 146 | struct RangeCheckImpl<Dst, Src, DST_UNSIGNED, SRC_SIGNED, OVERLAPS_RANGE> { |
| 147 | static RangeCheckResult Check(Src value) { |
| 148 | typedef std::numeric_limits<Dst> DstLimits; |
| 149 | typedef std::numeric_limits<Src> SrcLimits; |
| 150 | // Compare based on max_exponent, which we must compute for integrals. |
| 151 | static const size_t kDstMaxExponent = sizeof(Dst) * 8; |
Karl Wiberg | fd0de7a | 2017-11-07 11:02:40 +0100 | [diff] [blame] | 152 | static const size_t kSrcMaxExponent = |
| 153 | SrcLimits::is_iec559 ? SrcLimits::max_exponent : (sizeof(Src) * 8 - 1); |
| 154 | return (kDstMaxExponent >= kSrcMaxExponent) |
| 155 | ? BASE_NUMERIC_RANGE_CHECK_RESULT(true, |
| 156 | value >= static_cast<Src>(0)) |
| 157 | : BASE_NUMERIC_RANGE_CHECK_RESULT( |
| 158 | value <= static_cast<Src>(DstLimits::max()), |
| 159 | value >= static_cast<Src>(0)); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 160 | } |
| 161 | }; |
| 162 | |
| 163 | template <typename Dst, typename Src> |
| 164 | inline RangeCheckResult RangeCheck(Src value) { |
| 165 | static_assert(std::numeric_limits<Src>::is_specialized, |
| 166 | "argument must be numeric"); |
| 167 | static_assert(std::numeric_limits<Dst>::is_specialized, |
| 168 | "result must be numeric"); |
| 169 | return RangeCheckImpl<Dst, Src>::Check(value); |
| 170 | } |
| 171 | |
| 172 | } // namespace internal |
| 173 | } // namespace rtc |
Tommi | d44c077 | 2016-03-11 17:12:32 -0800 | [diff] [blame] | 174 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 175 | #endif // RTC_BASE_SAFE_CONVERSIONS_IMPL_H_ |