From: Jonathan Wakely Date: Thu, 29 May 2025 10:40:59 +0000 (+0100) Subject: libstdc++: Replace some implicit conversions in std::vector X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4db88b963b0966f14cd5bdab183f9bca355a0cd2;p=thirdparty%2Fgcc.git 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 --- diff --git a/libstdc++-v3/include/bits/stl_vector.h b/libstdc++-v3/include/bits/stl_vector.h index 5c0c227d8c4..f2c1bce1e38 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 801d9f049d0..70ead1d7083 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);