From patchwork Wed Dec 1 15:08:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 48366 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6F135385C412 for ; Wed, 1 Dec 2021 15:18:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6F135385C412 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1638371932; bh=+YHuLXSN8wRjh8Lp9aG2BJf25or0G6SUg7OWweWxYgc=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=ifiONUugmU8G70vwjp7Nr4gYW/xOqcyNvn+mq/REpyQ2hu7fCGWmcqRVIFVyuKI27 q497bg4JUV5pXSa0yxHUssRhSRxcdozz3mzACCLVgzUU6hoo0nnzNBfMD5J3NDwMP5 nLGkRVEjZRKKZxu1DkuzEGH78IgE+5oJeSZJUU+o= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 844003858034 for ; Wed, 1 Dec 2021 15:08:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 844003858034 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-353-DJnwuWmXOfi0xEVDg2TNuQ-1; Wed, 01 Dec 2021 10:08:55 -0500 X-MC-Unique: DJnwuWmXOfi0xEVDg2TNuQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7B3628BEEE0; Wed, 1 Dec 2021 15:08:36 +0000 (UTC) Received: from localhost (unknown [10.33.36.16]) by smtp.corp.redhat.com (Postfix) with ESMTP id 277ED77BEC; Wed, 1 Dec 2021 15:08:36 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Clear RB tree after moving elements [PR103501] Date: Wed, 1 Dec 2021 15:08:35 +0000 Message-Id: <20211201150835.217597-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, URI_HEX autolearn=unavailable autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Tested x86_64-linux, pushed to trunk. 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/map/allocator/move_cons.cc: Expect moved-from container to be empty. * testsuite/23_containers/multimap/allocator/move_cons.cc: Likewise. * testsuite/23_containers/multiset/allocator/103501.cc: New test. * testsuite/23_containers/set/allocator/103501.cc: New test. --- libstdc++-v3/include/bits/stl_tree.h | 6 ++-- .../23_containers/map/allocator/move_cons.cc | 2 +- .../multimap/allocator/move_cons.cc | 2 +- .../multiset/allocator/103501.cc | 32 +++++++++++++++++++ .../23_containers/set/allocator/103501.cc | 32 +++++++++++++++++++ 5 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 libstdc++-v3/testsuite/23_containers/multiset/allocator/103501.cc create mode 100644 libstdc++-v3/testsuite/23_containers/set/allocator/103501.cc diff --git a/libstdc++-v3/include/bits/stl_tree.h b/libstdc++-v3/include/bits/stl_tree.h index 0692525be57..55b8c9c7cb2 100644 --- a/libstdc++-v3/include/bits/stl_tree.h +++ b/libstdc++-v3/include/bits/stl_tree.h @@ -1644,9 +1644,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _M_move_data(__x, true_type()); else { + constexpr bool __move = !__move_if_noexcept_cond::value; _Alloc_node __an(*this); - _M_root() = - _M_copy::value>(__x, __an); + _M_root() = _M_copy<__move>(__x, __an); + if _GLIBCXX17_CONSTEXPR (__move) + __x.clear(); } } diff --git a/libstdc++-v3/testsuite/23_containers/map/allocator/move_cons.cc b/libstdc++-v3/testsuite/23_containers/map/allocator/move_cons.cc index 5beb26276b0..b82d3532135 100644 --- a/libstdc++-v3/testsuite/23_containers/map/allocator/move_cons.cc +++ b/libstdc++-v3/testsuite/23_containers/map/allocator/move_cons.cc @@ -41,7 +41,7 @@ void test01() VERIFY(1 == v1.get_allocator().get_personality()); VERIFY(2 == v2.get_allocator().get_personality()); - VERIFY( v1[1].empty() ); + VERIFY( v1.empty() ); VERIFY( v2[1] == str ); } diff --git a/libstdc++-v3/testsuite/23_containers/multimap/allocator/move_cons.cc b/libstdc++-v3/testsuite/23_containers/multimap/allocator/move_cons.cc index 91f2d0be508..37db0f005d1 100644 --- a/libstdc++-v3/testsuite/23_containers/multimap/allocator/move_cons.cc +++ b/libstdc++-v3/testsuite/23_containers/multimap/allocator/move_cons.cc @@ -41,7 +41,7 @@ void test01() VERIFY(1 == v1.get_allocator().get_personality()); VERIFY(2 == v2.get_allocator().get_personality()); - VERIFY( v1.begin()->second.empty() ); + VERIFY( v1.empty() ); VERIFY( v2.begin()->second == str ); } 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; + } +}