https://gcc.gnu.org/g:2848b8dabcaca05563b92528206345f8f845daff

commit r15-8903-g2848b8dabcaca05563b92528206345f8f845daff
Author: Jonathan Wakely <jwak...@redhat.com>
Date:   Mon Mar 24 21:21:02 2025 +0000

    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 <tkami...@redhat.com>

Diff:
---
 libstdc++-v3/include/debug/vector | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

diff --git a/libstdc++-v3/include/debug/vector 
b/libstdc++-v3/include/debug/vector
index 022ebe8c6645..b49766c18a76 100644
--- a/libstdc++-v3/include/debug/vector
+++ b/libstdc++-v3/include/debug/vector
@@ -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();
            }

Reply via email to