Skip to content

Commit bdcc6ef

Browse files
committed
Add hardening with bounded_iter
1 parent 1d98984 commit bdcc6ef

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

libcxx/cmake/caches/Generic-hardening-mode-fast-with-abi-breaks.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ set(_defines
55
_LIBCPP_ABI_BOUNDED_ITERATORS_IN_VECTOR
66
_LIBCPP_ABI_BOUNDED_UNIQUE_PTR
77
_LIBCPP_ABI_BOUNDED_ITERATORS_IN_STD_ARRAY
8+
_LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL
89
)
910
set(LIBCXX_ABI_DEFINES "${_defines}" CACHE STRING "")

libcxx/include/optional

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ namespace std {
205205
# include <__functional/invoke.h>
206206
# include <__functional/unary_function.h>
207207
# include <__fwd/functional.h>
208+
# include <__iterator/bounded_iter.h>
208209
# include <__iterator/wrap_iter.h>
209210
# include <__memory/addressof.h>
210211
# include <__memory/construct_at.h>
@@ -622,8 +623,13 @@ class _LIBCPP_DECLSPEC_EMPTY_BASES optional
622623
public:
623624
using value_type = _Tp;
624625
# if _LIBCPP_STD_VER >= 26
626+
# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL
627+
using iterator = __bounded_iter<__wrap_iter<pointer>>;
628+
using const_iterator = __bounded_iter<__wrap_iter<const_pointer>>;
629+
# else
625630
using iterator = __wrap_iter<pointer>;
626631
using const_iterator = __wrap_iter<const_pointer>;
632+
# endif
627633
# endif
628634

629635
using __trivially_relocatable _LIBCPP_NODEBUG =
@@ -831,10 +837,26 @@ public:
831837

832838
# if _LIBCPP_STD_VER >= 26
833839
// [optional.iterators], iterator support
834-
_LIBCPP_HIDE_FROM_ABI constexpr iterator begin() noexcept { return iterator(std::addressof(this->__get())); }
840+
_LIBCPP_HIDE_FROM_ABI constexpr iterator begin() noexcept {
841+
# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL
842+
return std::__make_bounded_iter(
843+
std::__wrap_iter<pointer>(std::addressof(this->__get())),
844+
std::__wrap_iter<pointer>(std::addressof(this->__get())),
845+
std::__wrap_iter<pointer>(std::addressof(this->__get()) + (this->has_value()) ? 1 : 0));
846+
# else
847+
return iterator(std::addressof(this->__get()));
848+
# endif
849+
}
835850

836851
_LIBCPP_HIDE_FROM_ABI constexpr const_iterator begin() const noexcept {
852+
# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL
853+
return std::__make_bounded_iter(
854+
std::__wrap_iter<const_pointer>(std::addressof(this->__get())),
855+
std::__wrap_iter<const_pointer>(std::addressof(this->__get())),
856+
std::__wrap_iter<const_pointer>(std::addressof(this->__get()) + (this->has_value()) ? 1 : 0));
857+
# else
837858
return const_iterator(std::addressof(this->__get()));
859+
# endif
838860
}
839861

840862
_LIBCPP_HIDE_FROM_ABI constexpr iterator end() noexcept { return begin() + (this->has_value() ? 1 : 0); }

0 commit comments

Comments
 (0)