safe_conversions_impl.h 7.0 KB

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