[PATCH] libstdc++: Replace some implicit conversions in std::vector
Tomasz Kaminski
tkaminsk@redhat.com
Tue Jun 3 08:38:27 GMT 2025
On Mon, Jun 2, 2025 at 7:33 PM Jonathan Wakely <jwakely@redhat.com> wrote:
> 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().
> ---
>
> Tested x86_64-linux.
>
LGTM.
>
> 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);
>
> --
> 2.49.0
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20250603/d0e779bf/attachment-0001.htm>
More information about the Libstdc++
mailing list