From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 885853881D2F; Tue, 28 Mar 2023 23:35:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 885853881D2F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1680046541; bh=yXdx92FydmGzr/HPDrSrR5Lje+YzAZH8VXmXjcxkF48=; h=From:To:Subject:Date:From; b=xle7Q+4efQZwA5GlrXfd3fmaRKAQvGOE2NDDd6ltZsVngOwz4MC2ura2hNkx8QHrO coulykn2rerqpsyKLRkURm8JWofrMxRQ+FIKnWvvf1ROMrxgKyn1zfWPhrd9AoVorW Sq9IiwSNgpTymkn/8fBjrNX4Mwa9zh4WrZKH7Vns= 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 r12-9351] libstdc++: Fix self-move for std::weak_ptr [PR108118] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: 7325d06f59da9a6fd3bea06e84bf93a8ee0c5bcb X-Git-Newrev: 629ed996f32cb03dd712789eede1f7f2036e497b Message-Id: <20230328233541.885853881D2F@sourceware.org> Date: Tue, 28 Mar 2023 23:35:41 +0000 (GMT) List-Id: https://gcc.gnu.org/g:629ed996f32cb03dd712789eede1f7f2036e497b commit r12-9351-g629ed996f32cb03dd712789eede1f7f2036e497b Author: Jonathan Wakely Date: Thu Dec 15 09:52:48 2022 +0000 libstdc++: Fix self-move for std::weak_ptr [PR108118] 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. (cherry picked from commit 92eb0adc14a5f84acce7e5bc780b81b1544b24aa) Diff: --- libstdc++-v3/include/bits/shared_ptr_base.h | 4 +--- .../testsuite/20_util/weak_ptr/cons/self_move.cc | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) 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(); +}