https://gcc.gnu.org/g:4db88b963b0966f14cd5bdab183f9bca355a0cd2

commit r16-1075-g4db88b963b0966f14cd5bdab183f9bca355a0cd2
Author: Jonathan Wakely <jwak...@redhat.com>
Date:   Thu May 29 11:40:59 2025 +0100

    libstdc++: Replace some implicit conversions in std::vector
    
    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 <tkami...@redhat.com>

Diff:
---
 libstdc++-v3/include/bits/stl_vector.h | 6 ++++--
 libstdc++-v3/include/bits/vector.tcc   | 2 +-
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/libstdc++-v3/include/bits/stl_vector.h 
b/libstdc++-v3/include/bits/stl_vector.h
index 5c0c227d8c49..f2c1bce1e386 100644
--- a/libstdc++-v3/include/bits/stl_vector.h
+++ b/libstdc++-v3/include/bits/stl_vector.h
@@ -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:
diff --git a/libstdc++-v3/include/bits/vector.tcc 
b/libstdc++-v3/include/bits/vector.tcc
index 801d9f049d02..70ead1d70836 100644
--- a/libstdc++-v3/include/bits/vector.tcc
+++ b/libstdc++-v3/include/bits/vector.tcc
@@ -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);

Reply via email to