]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Fix constexpr basic_string union member [PR113294]
authorPaul Keir <paul.keir@uws.ac.uk>
Mon, 12 Feb 2024 18:15:49 +0000 (18:15 +0000)
committerPatrick Palka <ppalka@redhat.com>
Mon, 12 Feb 2024 23:18:04 +0000 (18:18 -0500)
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>
libstdc++-v3/include/bits/basic_string.h
libstdc++-v3/testsuite/21_strings/basic_string/modifiers/constexpr.cc

index 43efc99bea9d9db5fd9e49b3c6c84d82d1297e5f..8a695a494efde0702d6aa3b17860a5be31e9537f 100644 (file)
@@ -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);
index 0e28a6d448725fdf7d265087ac324dc8c72c9614..d7022af3cabe981fbc0d682fd893c5e2e578ec7b 100644 (file)
@@ -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() );