[libcxx-commits] [libcxx] [libc++][hardening] Check bounds on arithmetic in __bounded_iter (PR #78876)
Will Hawkins via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Feb 20 22:20:01 PST 2024
================
@@ -90,30 +99,37 @@ struct __bounded_iter {
public:
// Dereference and indexing operations.
//
- // These operations check that the iterator is dereferenceable, that is within [begin, end).
+ // These operations check that the iterator is dereferenceable. Since the class invariant is
+ // that the iterator is always within `[begin, end]`, we only need to check it's not pointing to
+ // `end`. This is easier for the optimizer because it aligns with the `iter != container.end()`
+ // checks that typical callers already use (see
+ // https://github.com/llvm/llvm-project/issues/78829).
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator*() const _NOEXCEPT {
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
- __in_bounds(__current_), "__bounded_iter::operator*: Attempt to dereference an out-of-range iterator");
+ __current_ != __end_, "__bounded_iter::operator*: Attempt to dereference an iterator at the end");
return *__current_;
}
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pointer operator->() const _NOEXCEPT {
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
- __in_bounds(__current_), "__bounded_iter::operator->: Attempt to dereference an out-of-range iterator");
+ __current_ != __end_, "__bounded_iter::operator->: Attempt to dereference an iterator at the end");
return std::__to_address(__current_);
}
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator[](difference_type __n) const _NOEXCEPT {
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
- __in_bounds(__current_ + __n), "__bounded_iter::operator[]: Attempt to index an iterator out-of-range");
+ __n >= __begin_ - __current_, "__bounded_iter::operator[]: Attempt to index an iterator past the start");
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
+ __n < __end_ - __current_, "__bounded_iter::operator[]: Attempt to index an iterator at or past the end");
return __current_[__n];
}
// Arithmetic operations.
//
- // These operations do not check that the resulting iterator is within the bounds, since that
- // would make it impossible to create a past-the-end iterator.
+ // These operations check the iterator remains within `[begin, end]`.
----------------
hawkinsw wrote:
```suggestion
// These operations check that the iterator remains within `[begin, end]`.
```
https://github.com/llvm/llvm-project/pull/78876
More information about the libcxx-commits
mailing list