From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1888) id 807343858C2C; Mon, 12 Feb 2024 23:19:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 807343858C2C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1707779948; bh=bnLjOOFKW8xnRp/K8gq872Bj+suGfUO6ZupYEO37nvQ=; h=From:To:Subject:Date:From; b=pilkoxKz4VVjUbawTn9MAQqREN1mcMAALCqDqB+w9KEGJJSN1w+thx1I6AvOGLQVY diUNW2qg8/xtpupaXAuCMZKd64abwdFFkxfQZbZOKRJDgE2HMyPZT8vNZ3iaVd/Dc9 MjqHg4xYBfrgMNdGm9ShXjurMfhYf7m4gNDA5tCc= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Patrick Palka To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r14-8943] libstdc++: Fix constexpr basic_string union member [PR113294] X-Act-Checkin: gcc X-Git-Author: Paul Keir X-Git-Refname: refs/heads/master X-Git-Oldrev: 39d989022dd0eacf1a7b95b7b20621acbe717d70 X-Git-Newrev: 065dddc6e07a917c57c7955db13b1fe77abbcabc Message-Id: <20240212231908.807343858C2C@sourceware.org> Date: Mon, 12 Feb 2024 23:19:08 +0000 (GMT) List-Id: https://gcc.gnu.org/g:065dddc6e07a917c57c7955db13b1fe77abbcabc commit r14-8943-g065dddc6e07a917c57c7955db13b1fe77abbcabc Author: Paul Keir Date: Mon Feb 12 18:15:49 2024 +0000 libstdc++: Fix constexpr basic_string union member [PR113294] A call to `basic_string::clear()` in the std::string move assignment operator leads to a constexpr error from an access of inactive union member `_M_local_buf` in the added test (`test_move()`). Changing `__str._M_local_buf` to `__str._M_use_local_data()` in `operator=(basic_string&& __str)` fixes this. PR libstdc++/113294 libstdc++-v3/ChangeLog: * include/bits/basic_string.h (basic_string::operator=): Use _M_use_local_data() instead of _M_local_buf on the moved-from string. * testsuite/21_strings/basic_string/modifiers/constexpr.cc (test_move): New test. Signed-off-by: Paul Keir Reviewed-by: Patrick Palka Reviewed-by: Jonathan Wakely Diff: --- libstdc++-v3/include/bits/basic_string.h | 2 +- .../21_strings/basic_string/modifiers/constexpr.cc | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index 43efc99bea9d..8a695a494efd 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -909,7 +909,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 __str._M_capacity(__capacity); } else - __str._M_data(__str._M_local_buf); + __str._M_data(__str._M_use_local_data()); } else // Need to do a deep copy assign(__str); diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/modifiers/constexpr.cc b/libstdc++-v3/testsuite/21_strings/basic_string/modifiers/constexpr.cc index 0e28a6d44872..d7022af3cabe 100644 --- a/libstdc++-v3/testsuite/21_strings/basic_string/modifiers/constexpr.cc +++ b/libstdc++-v3/testsuite/21_strings/basic_string/modifiers/constexpr.cc @@ -50,3 +50,17 @@ test_erasure() } static_assert( test_erasure() ); + +constexpr bool +test_move() +{ + // PR libstdc++/113294 + std::string s1; + std::string s2 = "1234567890123456"; // 16 chars: more than _S_local_capacity + s1 = std::move(s2); + VERIFY( s1 == "1234567890123456" ); + + return true; +} + +static_assert( test_move() );