On Fri, 24 Nov 2023 at 20:07, Jan Hubicka <[email protected]> wrote:
> The vector.tcc change was regtested on x86_64-linux, OK?
>
> libstdc++-v3/ChangeLog:
>
> * include/bits/vector.tcc (reserve): Copy _M_start and _M_finish
> to local variables to allow propagation across call to
> allocator.
>
> diff --git a/libstdc++-v3/include/bits/vector.tcc
> b/libstdc++-v3/include/bits/vector.tcc
> index 0ccef7911b3..0a9db29c1c7 100644
> --- a/libstdc++-v3/include/bits/vector.tcc
> +++ b/libstdc++-v3/include/bits/vector.tcc
> @@ -72,27 +72,30 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
> if (this->capacity() < __n)
> {
> const size_type __old_size = size();
> + // Make local copies of these members because the compiler thinks
> + // the allocator can alter them if 'this' is globally reachable.
> + pointer __old_start = this->_M_impl._M_start;
> + pointer __old_finish = this->_M_impl._M_finish;
> pointer __tmp;
> #if __cplusplus >= 201103L
> if _GLIBCXX17_CONSTEXPR (_S_use_relocate())
> {
> __tmp = this->_M_allocate(__n);
> - _S_relocate(this->_M_impl._M_start, this->_M_impl._M_finish,
> + _S_relocate(__old_start, __old_finish,
> __tmp, _M_get_Tp_allocator());
Please move "__tmp," to the first line, as it will fit there now that
the line got shorter:
_S_relocate(__old_start, __old_finish, __tmp,
_M_get_Tp_allocator());
> }
> else
> #endif
> {
> __tmp = _M_allocate_and_copy(__n,
> -
> _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(this->_M_impl._M_start),
> -
> _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(this->_M_impl._M_finish));
> - std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
> + _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(__old_start),
> + _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(__old_finish));
> + std::_Destroy(__old_start, __old_finish,
> _M_get_Tp_allocator());
The _Destroy call will fit on one line now:
std::_Destroy(__old_start, __old_finish, _M_get_Tp_allocator());
> }
> _GLIBCXX_ASAN_ANNOTATE_REINIT;
> - _M_deallocate(this->_M_impl._M_start,
> - this->_M_impl._M_end_of_storage
> - - this->_M_impl._M_start);
> + _M_deallocate(__old_start,
> + this->_M_impl._M_end_of_storage - __old_finish);
This should be __old_start.
I think what you have here will show Asan and/or valgrind errors, as
the wrong length will be passed to operator delete.
> this->_M_impl._M_start = __tmp;
> this->_M_impl._M_finish = __tmp + __old_size;
> this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
>