]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Adjust how __gnu_debug::vector detects invalidation
authorJonathan Wakely <jwakely@redhat.com>
Mon, 24 Mar 2025 21:21:02 +0000 (21:21 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Tue, 25 Mar 2025 17:38:39 +0000 (17:38 +0000)
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>
libstdc++-v3/include/debug/vector

index 022ebe8c66456dc2ad5ab10a0c7813420223eab8..b49766c18a76ea2a68b4bbeaaac8343660d31be1 100644 (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();
            }