libstdc++: Adjust how __gnu_debug::vector detects invalidation

The new C++23 member functions assign_range, insert_range and
append_range were checking whether the begin() iterator changed after
calling the base class member. That works, but is technically undefined
when the original iterator has been invalidated by a change in capacity.

We can just check the capacity directly, because reallocation only
occurs if a change in capacity is required.

N.B. we can't use data() either because std::vector<bool> doesn't have
it.

libstdc++-v3/ChangeLog:

	* include/debug/vector (vector::assign_range): Use change in
	capacity to detect reallocation.
	(vector::insert_range, vector::append_range): Likewise. Remove
	unused variables.

Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
This commit is contained in:
Jonathan Wakely 2025-03-24 21:21:02 +00:00 committed by Jonathan Wakely
parent d4f7d18b3e
commit 2848b8dabc
No known key found for this signature in database

View file

@ -876,12 +876,12 @@ namespace __debug
constexpr void
assign_range(_Rg&& __rg)
{
auto __old_begin = _Base::begin();
auto __old_capacity = _Base::capacity();
auto __old_size = _Base::size();
_Base::assign_range(__rg);
if (!std::__is_constant_evaluated())
{
if (_Base::begin() != __old_begin)
if (_Base::capacity() != __old_capacity)
this->_M_invalidate_all();
else if (_Base::size() < __old_size)
this->_M_invalidate_after_nth(_Base::size());
@ -893,12 +893,11 @@ namespace __debug
constexpr iterator
insert_range(const_iterator __pos, _Rg&& __rg)
{
auto __old_begin = _Base::begin();
auto __old_size = _Base::size();
auto __old_capacity = _Base::capacity();
auto __res = _Base::insert_range(__pos.base(), __rg);
if (!std::__is_constant_evaluated())
{
if (_Base::begin() != __old_begin)
if (_Base::capacity() != __old_capacity)
this->_M_invalidate_all();
this->_M_update_guaranteed_capacity();
}
@ -909,12 +908,11 @@ namespace __debug
constexpr void
append_range(_Rg&& __rg)
{
auto __old_begin = _Base::begin();
auto __old_size = _Base::size();
auto __old_capacity = _Base::capacity();
_Base::append_range(__rg);
if (!std::__is_constant_evaluated())
{
if (_Base::begin() != __old_begin)
if (_Base::capacity() != __old_capacity)
this->_M_invalidate_all();
this->_M_update_guaranteed_capacity();
}