From 318c48e30402eadaeee86dee9486733678c5fef4 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Thu, 6 Jul 2017 12:54:10 +0100 Subject: [PATCH] Prevent __uses_alloc from holding dangling references * include/bits/uses_allocator.h (__use_alloc(const _Alloc&&)): Add deleted overload to prevent dangling references to rvalues. * include/experimental/memory_resource (polymorphic_allocator::construct): Do not call __use_alloc with rvalue arguments. From-SVN: r250019 --- libstdc++-v3/ChangeLog | 8 ++++++++ libstdc++-v3/include/bits/uses_allocator.h | 5 +++++ libstdc++-v3/include/experimental/memory_resource | 10 ++++++---- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 770b5789da7..1c50a3b2b4c 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,11 @@ +2017-07-06 Jonathan Wakely + + * include/bits/uses_allocator.h (__use_alloc(const _Alloc&&)): Add + deleted overload to prevent dangling references to rvalues. + * include/experimental/memory_resource + (polymorphic_allocator::construct): Do not call __use_alloc with + rvalue arguments. + 2017-06-27 Tim Shen PR libstdc++/80187 diff --git a/libstdc++-v3/include/bits/uses_allocator.h b/libstdc++-v3/include/bits/uses_allocator.h index 89d4e435659..4d60716b494 100644 --- a/libstdc++-v3/include/bits/uses_allocator.h +++ b/libstdc++-v3/include/bits/uses_allocator.h @@ -109,6 +109,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __ret._M_a = std::__addressof(__a); return __ret; } + + template + void + __use_alloc(const _Alloc&&) = delete; + #if __cplusplus > 201402L template inline constexpr bool uses_allocator_v = diff --git a/libstdc++-v3/include/experimental/memory_resource b/libstdc++-v3/include/experimental/memory_resource index 653189c1079..99ace7a7f81 100644 --- a/libstdc++-v3/include/experimental/memory_resource +++ b/libstdc++-v3/include/experimental/memory_resource @@ -168,8 +168,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template //used here void construct(_Tp1* __p, _Args&&... __args) { - auto __use_tag = __use_alloc<_Tp1, memory_resource*, - _Args...>(this->resource()); + memory_resource* const __resource = this->resource(); + auto __use_tag + = __use_alloc<_Tp1, memory_resource*, _Args...>(__resource); _M_construct(__use_tag, __p, std::forward<_Args>(__args)...); } @@ -180,10 +181,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION tuple<_Args1...> __x, tuple<_Args2...> __y) { + memory_resource* const __resource = this->resource(); auto __x_use_tag = - __use_alloc<_Tp1, memory_resource*, _Args1...>(this->resource()); + __use_alloc<_Tp1, memory_resource*, _Args1...>(__resource); auto __y_use_tag = - __use_alloc<_Tp2, memory_resource*, _Args2...>(this->resource()); + __use_alloc<_Tp2, memory_resource*, _Args2...>(__resource); ::new(__p) std::pair<_Tp1, _Tp2>(piecewise_construct, _M_construct_p(__x_use_tag, __x), -- 2.30.2