From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 8310A3858C2C; Wed, 15 Jun 2022 08:14:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8310A3858C2C MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r10-10837] libstdc++: Clear RB tree after moving elements [PR103501] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-10 X-Git-Oldrev: 24551c8642150bbf51cdb1f740ad59a1a57be294 X-Git-Newrev: d4fdb293004f2e104edf823d4820d4ee73aa2660 Message-Id: <20220615081439.8310A3858C2C@sourceware.org> Date: Wed, 15 Jun 2022 08:14:39 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 15 Jun 2022 08:14:39 -0000 https://gcc.gnu.org/g:d4fdb293004f2e104edf823d4820d4ee73aa2660 commit r10-10837-gd4fdb293004f2e104edf823d4820d4ee73aa2660 Author: Jonathan Wakely Date: Tue Nov 30 23:32:50 2021 +0000 libstdc++: Clear RB tree after moving elements [PR103501] If the allocator-extended move constructor move-constructs each element into the new container, the contents of the old container are left in moved-from states. We cannot know if those states preserve the container's ordering and uniqueness guarantees, so just erase all moved-from elements. libstdc++-v3/ChangeLog: PR libstdc++/103501 * include/bits/stl_tree.h (_Rb_tree(_Rb_tree&&, false_type)): Clear container if elements have been moved-from. * testsuite/23_containers/multiset/allocator/103501.cc: New test. * testsuite/23_containers/set/allocator/103501.cc: New test. (cherry picked from commit 056551414a328b427c4bf4955b9a3832f344685c) Diff: --- libstdc++-v3/include/bits/stl_tree.h | 3 ++ .../23_containers/multiset/allocator/103501.cc | 32 ++++++++++++++++++++++ .../23_containers/set/allocator/103501.cc | 32 ++++++++++++++++++++++ 3 files changed, 67 insertions(+) diff --git a/libstdc++-v3/include/bits/stl_tree.h b/libstdc++-v3/include/bits/stl_tree.h index 21b72cebf2e..1717ccbfff4 100644 --- a/libstdc++-v3/include/bits/stl_tree.h +++ b/libstdc++-v3/include/bits/stl_tree.h @@ -1669,6 +1669,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _M_move_data(__x, true_type()); else { + constexpr bool __move = !__move_if_noexcept_cond::value; _Alloc_node __an(*this); auto __lbd = [&__an](const value_type& __cval) @@ -1677,6 +1678,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return __an(std::move_if_noexcept(__val)); }; _M_root() = _M_copy(__x, __lbd); + if _GLIBCXX17_CONSTEXPR (__move) + __x.clear(); } } diff --git a/libstdc++-v3/testsuite/23_containers/multiset/allocator/103501.cc b/libstdc++-v3/testsuite/23_containers/multiset/allocator/103501.cc new file mode 100644 index 00000000000..24f657eceba --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/multiset/allocator/103501.cc @@ -0,0 +1,32 @@ +// { dg-do run { target c++11 } } + +// PR libstdc++/103501 + +#include +#include +#include + +struct Y +{ + int i; + + Y(int i) : i(i) { } + Y(const Y& y) noexcept : i(y.i) { } + Y(Y&& y) noexcept : i(y.i) { y.i = -y.i; } + + bool operator<(const Y& rhs) const { return i < rhs.i; } +}; + +int main() +{ + using Alloc = __gnu_test::uneq_allocator; + std::multiset, Alloc> s1{ {1, 2, 3}, Alloc(1)}; + std::multiset, Alloc> s2{ std::move(s1), Alloc(2) }; + const Y* prev = nullptr; + for (const Y& y : s1) + { + if (prev) + VERIFY( !(y < *prev) ); + prev = &y; + } +} diff --git a/libstdc++-v3/testsuite/23_containers/set/allocator/103501.cc b/libstdc++-v3/testsuite/23_containers/set/allocator/103501.cc new file mode 100644 index 00000000000..7267cf9663f --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/set/allocator/103501.cc @@ -0,0 +1,32 @@ +// { dg-do run { target c++11 } } + +// PR libstdc++/103501 + +#include +#include +#include + +struct X +{ + int i; + + X(int i) : i(i) { } + X(const X& x) noexcept : i(x.i) { } + X(X&& x) noexcept : i(x.i) { x.i = -1; } + + bool operator<(const X& rhs) const { return i < rhs.i; } +}; + +int main() +{ + using Alloc = __gnu_test::uneq_allocator; + std::set, Alloc> s1{ {1, 2, 3}, Alloc(1)}; + std::set, Alloc> s2{ std::move(s1), Alloc(2) }; + const X* prev = nullptr; + for (const X& x : s1) + { + if (prev) + VERIFY( *prev < x ); + prev = &x; + } +}