]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Remove noexcept from non-const std::basic_string::data() [PR99942]
authorJonathan Wakely <jwakely@redhat.com>
Wed, 27 Mar 2024 11:07:17 +0000 (11:07 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Thu, 1 Aug 2024 20:56:55 +0000 (21:56 +0100)
The C++17 non-const overload of data() allows modifying the string
contents directly, so for the COW string we must do a copy-on-write to
unshare it. That means allocating, which can throw, so it shouldn't be
noexcept.

libstdc++-v3/ChangeLog:

PR libstdc++/99942
* include/bits/cow_string.h (data()): Change to noexcept(false).

libstdc++-v3/include/bits/cow_string.h

index 5d81bfc1230e44fc841098148586f6ca1e4770d1..75a2d887ad632f1114ec58acf3f06fc78ccca39b 100644 (file)
@@ -2267,9 +2267,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *
        *  This is a pointer to the character sequence held by the string.
        *  Modifying the characters in the sequence is allowed.
+       *
+       *  The standard requires this function to be `noexcept` but for the
+       *  Copy-On-Write string implementation it can throw.  This function
+       *  allows modifying the string contents directly, which means we
+       *  must copy-on-write to unshare it, which requires allocating memory.
       */
       _CharT*
-      data() noexcept
+      data() noexcept(false)
       {
        _M_leak();
        return _M_data();