Chromium Code Reviews| 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 base::CheckedNumeric<ValueType> checked_base_(base_); | |
|
Robert Sesek
2014/08/14 20:55:52
No underscore at the end of checked_base_, or on l
| |
| 63 if (!base::IsValueInRangeForNumericType<ValueType, SizeType>(size_)) { | |
| 64 return false; | |
| 65 } | |
| 66 base::CheckedNumeric<ValueType> checked_end_(checked_base_ + | |
| 67 static_cast<ValueType>(size_)); | |
|
Robert Sesek
2014/08/14 20:55:52
This read "fishy" for me for me, so I spent 10 min
Mark Mentovai
2014/08/14 23:12:01
rsesek wrote:
| |
| 68 return checked_end_.IsValid(); | |
| 69 } | |
| 70 | |
| 71 //! \brief Returns whether the range contains another value. | |
| 72 //! | |
| 73 //! \param[in] value The (possibly) contained value. | |
| 74 //! | |
| 75 //! \return `true` if the range contains \a value, `false` otherwise. | |
| 76 //! | |
| 77 //! A range contains a value if the value is greater than or equal to its | |
| 78 //! base, and less than its end (base plus size). | |
| 79 //! | |
| 80 //! This method must only be called if IsValid() would return `true`. | |
| 81 bool ContainsValue(ValueType value) const { | |
| 82 DCHECK(IsValid()); | |
| 83 | |
| 84 return value >= base_ && value < end(); | |
|
Robert Sesek
2014/08/14 20:55:52
Why base_ but end() ?
| |
| 85 } | |
| 86 | |
| 87 //! \brief Returns whether the range contains another range. | |
| 88 //! | |
| 89 //! \param[in] that The (possibly) contained range. | |
| 90 //! | |
| 91 //! \return `true` if `this` range, the containing range, contains \a that, | |
| 92 //! the contained range. `false` otherwise. | |
| 93 //! | |
| 94 //! A range contains another range when the contained range’s base is greater | |
| 95 //! than or equal to the containing range’s base, and the contained range’s | |
| 96 //! end is less than or equal to the containing range’s end. | |
| 97 //! | |
| 98 //! This method must only be called if IsValid() would return `true` for both | |
| 99 //! CheckedRange objects involved. | |
| 100 bool ContainsRange(const CheckedRange<ValueType, SizeType>& that) const { | |
| 101 DCHECK(IsValid()); | |
| 102 DCHECK(that.IsValid()); | |
| 103 | |
| 104 return that.base_ >= base_ && that.end() <= end(); | |
|
Robert Sesek
2014/08/14 20:55:52
Same.
| |
| 105 } | |
| 106 | |
| 107 private: | |
| 108 ValueType base_; | |
| 109 SizeType size_; | |
| 110 | |
| 111 DISALLOW_COPY_AND_ASSIGN(CheckedRange); | |
| 112 }; | |
| 113 | |
| 114 } // namespace crashpad | |
| 115 | |
| 116 #endif // CRASHPAD_UTIL_NUMERIC_CHECKED_RANGE_H_ | |
| OLD | NEW |