]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Replace some implicit conversions in std::vector
authorJonathan Wakely <jwakely@redhat.com>
Thu, 29 May 2025 10:40:59 +0000 (11:40 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Tue, 3 Jun 2025 09:53:00 +0000 (10:53 +0100)
This replaces two implicit conversions from ptrdiff_t to size_t with
explicit conversions that include unreachable hints for the ptrdiff_t
value not being negative.

libstdc++-v3/ChangeLog:

* include/bits/stl_vector.h (~_Vector_base): Add unreachable
hint for negative capacity and cast to size_t explicitly.
* include/bits/vector.tcc (vector::_M_realloc_append): Use
size() instead of end() - begin().

Reviewed-by: Tomasz KamiƄski <tkaminsk@redhat.com>
libstdc++-v3/include/bits/stl_vector.h
libstdc++-v3/include/bits/vector.tcc

index 5c0c227d8c490e7732fa0fc69189d8424eb4c659..f2c1bce1e386913ccdafca386a96cc23af76d0e1 100644 (file)
@@ -372,8 +372,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       _GLIBCXX20_CONSTEXPR
       ~_Vector_base() _GLIBCXX_NOEXCEPT
       {
-       _M_deallocate(_M_impl._M_start,
-                     _M_impl._M_end_of_storage - _M_impl._M_start);
+       ptrdiff_t __n = _M_impl._M_end_of_storage - _M_impl._M_start;
+       if (__n < 0)
+         __builtin_unreachable();
+       _M_deallocate(_M_impl._M_start, size_t(__n));
       }
 
     public:
index 801d9f049d02e7a84adf50fd56ff264d8e0c6d32..70ead1d70836dc4fb20e619cd44adbe14605ebad 100644 (file)
@@ -576,7 +576,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
        __builtin_unreachable();
       pointer __old_start = this->_M_impl._M_start;
       pointer __old_finish = this->_M_impl._M_finish;
-      const size_type __elems = end() - begin();
+      const size_type __elems = size();
       pointer __new_start(this->_M_allocate(__len));
       pointer __new_finish(__new_start);