OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #ifndef CRASHPAD_UTIL_NUMERIC_CHECKED_RANGE_H_ |
| 16 #define CRASHPAD_UTIL_NUMERIC_CHECKED_RANGE_H_ |
| 17 |
| 18 #include <limits> |
| 19 |
| 20 #include "base/basictypes.h" |
| 21 #include "base/logging.h" |
| 22 #include "base/numerics/safe_conversions.h" |
| 23 #include "base/numerics/safe_math.h" |
| 24 |
| 25 namespace crashpad { |
| 26 |
| 27 //! \brief Ensures that a range, composed of a base and size, does not overflow |
| 28 //! its data type. |
| 29 template <typename ValueType, typename SizeType = ValueType> |
| 30 class CheckedRange { |
| 31 public: |
| 32 CheckedRange(ValueType base, SizeType size) { |
| 33 COMPILE_ASSERT(!std::numeric_limits<SizeType>::is_signed, |
| 34 SizeType_must_be_unsigned); |
| 35 SetRange(base, size); |
| 36 } |
| 37 |
| 38 //! \brief Sets the range’s base and size to \a base and \a size, |
| 39 //! respectively. |
| 40 void SetRange(ValueType base, SizeType size) { |
| 41 base_ = base; |
| 42 size_ = size; |
| 43 } |
| 44 |
| 45 //! \brief The range’s base. |
| 46 ValueType base() const { return base_; } |
| 47 |
| 48 //! \brief The range’s size. |
| 49 SizeType size() const { return size_; } |
| 50 |
| 51 //! \brief The range’s end, its base plus its size. |
| 52 ValueType end() const { return base_ + size_; } |
| 53 |
| 54 //! \brief Returns the validity of the range. |
| 55 //! |
| 56 //! \return `true` if the range is valid, `false` otherwise. |
| 57 //! |
| 58 //! A range is valid if its size can be converted to the range’s data type |
| 59 //! without data loss, and if its end (base plus size) can be computed without |
| 60 //! overflowing its data type. |
| 61 bool IsValid() const { |
| 62 if (!base::IsValueInRangeForNumericType<ValueType, SizeType>(size_)) { |
| 63 return false; |
| 64 } |
| 65 base::CheckedNumeric<ValueType> checked_end(base_); |
| 66 checked_end += static_cast<ValueType>(size_); |
| 67 return checked_end.IsValid(); |
| 68 } |
| 69 |
| 70 //! \brief Returns whether the range contains another value. |
| 71 //! |
| 72 //! \param[in] value The (possibly) contained value. |
| 73 //! |
| 74 //! \return `true` if the range contains \a value, `false` otherwise. |
| 75 //! |
| 76 //! A range contains a value if the value is greater than or equal to its |
| 77 //! base, and less than its end (base plus size). |
| 78 //! |
| 79 //! This method must only be called if IsValid() would return `true`. |
| 80 bool ContainsValue(ValueType value) const { |
| 81 DCHECK(IsValid()); |
| 82 |
| 83 return value >= base() && value < end(); |
| 84 } |
| 85 |
| 86 //! \brief Returns whether the range contains another range. |
| 87 //! |
| 88 //! \param[in] that The (possibly) contained range. |
| 89 //! |
| 90 //! \return `true` if `this` range, the containing range, contains \a that, |
| 91 //! the contained range. `false` otherwise. |
| 92 //! |
| 93 //! A range contains another range when the contained range’s base is greater |
| 94 //! than or equal to the containing range’s base, and the contained range’s |
| 95 //! end is less than or equal to the containing range’s end. |
| 96 //! |
| 97 //! This method must only be called if IsValid() would return `true` for both |
| 98 //! CheckedRange objects involved. |
| 99 bool ContainsRange(const CheckedRange<ValueType, SizeType>& that) const { |
| 100 DCHECK(IsValid()); |
| 101 DCHECK(that.IsValid()); |
| 102 |
| 103 return that.base() >= base() && that.end() <= end(); |
| 104 } |
| 105 |
| 106 private: |
| 107 ValueType base_; |
| 108 SizeType size_; |
| 109 |
| 110 DISALLOW_COPY_AND_ASSIGN(CheckedRange); |
| 111 }; |
| 112 |
| 113 } // namespace crashpad |
| 114 |
| 115 #endif // CRASHPAD_UTIL_NUMERIC_CHECKED_RANGE_H_ |
OLD | NEW |