From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 A93683831E4C for ; Fri, 16 Dec 2022 14:31:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A93683831E4C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1671201070; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=wRfRWl1Vw+fvH0bc9VRd9hNQYxJYNnFW665petq6EEM=; b=bExflXji9mWNprcUYTltt112oiOCnjAxgNZioAyyYFIxdxjBiZYZ1TatBXxf6+hxYoTpzL ObMYveh/PPwOuOo3FvX/OAbXujRQqgXqWSsqu5gFRloK/+jojt7RDPmErRPbhAetR2Y67y gQA+3clSUYTAPz9US1iMPwcvja7JeV4= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-497-t9uk7bZQOWCLttOMYVs-OA-1; Fri, 16 Dec 2022 09:31:06 -0500 X-MC-Unique: t9uk7bZQOWCLttOMYVs-OA-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9EF752999B21; Fri, 16 Dec 2022 14:31:06 +0000 (UTC) Received: from localhost (unknown [10.33.36.175]) by smtp.corp.redhat.com (Postfix) with ESMTP id 65FFF49BB6A; Fri, 16 Dec 2022 14:31:06 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix self-move for std::weak_ptr [PR108118] Date: Fri, 16 Dec 2022 14:31:05 +0000 Message-Id: <20221216143105.118483-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.2 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_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Tested x86_64-lixnu. Pushed to trunk. I'll backport this too. -- >8 -- I think an alternative fix would be something like: _M_ptr = std::exchange(rhs._M_ptr, nullptr); _M_refcount = std::move(rhs._M_refcount); The standard's move-and-swap implementation generates smaller code at all levels except -O0 and -Og, so it seems simplest to just do what the standard says. libstdc++-v3/ChangeLog: PR libstdc++/108118 * include/bits/shared_ptr_base.h (weak_ptr::operator=): Implement as move-and-swap exactly as specified in the standard. * testsuite/20_util/weak_ptr/cons/self_move.cc: New test. --- libstdc++-v3/include/bits/shared_ptr_base.h | 4 +--- .../20_util/weak_ptr/cons/self_move.cc | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/weak_ptr/cons/self_move.cc diff --git a/libstdc++-v3/include/bits/shared_ptr_base.h b/libstdc++-v3/include/bits/shared_ptr_base.h index d9230b72203..c22b397a194 100644 --- a/libstdc++-v3/include/bits/shared_ptr_base.h +++ b/libstdc++-v3/include/bits/shared_ptr_base.h @@ -2049,9 +2049,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __weak_ptr& operator=(__weak_ptr&& __r) noexcept { - _M_ptr = __r._M_ptr; - _M_refcount = std::move(__r._M_refcount); - __r._M_ptr = nullptr; + __weak_ptr(std::move(__r)).swap(*this); return *this; } diff --git a/libstdc++-v3/testsuite/20_util/weak_ptr/cons/self_move.cc b/libstdc++-v3/testsuite/20_util/weak_ptr/cons/self_move.cc new file mode 100644 index 00000000000..c890d2ba94d --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/weak_ptr/cons/self_move.cc @@ -0,0 +1,19 @@ +// { dg-do run { target c++11 } } + +#include +#include + +void +test_self_move() +{ + std::shared_ptr sp(new int(66)); + std::weak_ptr wp(sp); + wp = std::move(wp); // PR libstdc++/108118 + std::shared_ptr sp2(wp); + VERIFY(sp2 == sp); +} + +int main() +{ + test_self_move(); +} -- 2.38.1