public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Patrick Palka <ppalka@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r13-8335] libstdc++: Fix constexpr basic_string union member [PR113294]
Date: Fri, 16 Feb 2024 15:21:11 +0000 (GMT)	[thread overview]
Message-ID: <20240216152111.438F9385829E@sourceware.org> (raw)

https://gcc.gnu.org/g:ebe00c9d3a0436dec5c354a62d98e444d763ff95

commit r13-8335-gebe00c9d3a0436dec5c354a62d98e444d763ff95
Author: Paul Keir <paul.keir@uws.ac.uk>
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 <paul.keir@uws.ac.uk>
    Reviewed-by: Patrick Palka <ppalka@redhat.com>
    Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
    (cherry picked from commit 065dddc6e07a917c57c7955db13b1fe77abbcabc)

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 b73991d96da1..36598803f00c 100644
--- a/libstdc++-v3/include/bits/basic_string.h
+++ b/libstdc++-v3/include/bits/basic_string.h
@@ -904,7 +904,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 77fe59bb7858..2c12fdf42409 100644
--- a/libstdc++-v3/testsuite/21_strings/basic_string/modifiers/constexpr.cc
+++ b/libstdc++-v3/testsuite/21_strings/basic_string/modifiers/constexpr.cc
@@ -51,3 +51,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() );

                 reply	other threads:[~2024-02-16 15:21 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240216152111.438F9385829E@sourceware.org \
    --to=ppalka@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).