diff mbox series

[committed] libstdc++: Remove noexcept from non-const std::basic_string::data() [PR99942]

Message ID 20240801210853.385911-1-jwakely@redhat.com
State New
Headers show
Series [committed] libstdc++: Remove noexcept from non-const std::basic_string::data() [PR99942] | expand

Commit Message

Jonathan Wakely Aug. 1, 2024, 9:07 p.m. UTC
I've had this in my local tree for ages, time to push it I think. It's
better to be non-conforming than to lie and posibly terminate the whole
process (the COW std::string is already non-conforming in C++11 and
later anyway).

Tested x86_64-linux. Pushed to trunk.

-- >8 --

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 | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/libstdc++-v3/include/bits/cow_string.h b/libstdc++-v3/include/bits/cow_string.h
index 5d81bfc1230..75a2d887ad6 100644
--- a/libstdc++-v3/include/bits/cow_string.h
+++ b/libstdc++-v3/include/bits/cow_string.h
@@ -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();