Changes in v2:
 - Remove helper function, use -fnoaccess-control and call
  _M_create directly instead.
 - Build test with -fno-ipa-* flags.
 - Add check for capacity 2.
 - Scrub opinion about _M_create's semantics.

acfdad706d8 added a basic_string member _M_create_plus to use
when allocating string storage, but retained the old _M_create
symbol so that object files built against older releases would
still find it. However, code built into some old object files
relies on _M_create to allocate no more bytes than requested
when the old capacity is zero, something it no longer does.

This patch restores _M_create's exact previous behavior, to
rescue those old object files.

libstdc++-v3/ChangeLog:
        PR libstdc++/127228
        * include/bits/basic_string.tcc (_M_create): Restore old version.
        * testsuite/21_strings/basic_string/127228.cc: New test.
---
 libstdc++-v3/include/bits/basic_string.tcc    | 12 ++++++---
 .../21_strings/basic_string/127228.cc         | 25 +++++++++++++++++++
 2 files changed, 34 insertions(+), 3 deletions(-)
 create mode 100644 libstdc++-v3/testsuite/21_strings/basic_string/127228.cc

diff --git a/libstdc++-v3/include/bits/basic_string.tcc 
b/libstdc++-v3/include/bits/basic_string.tcc
index 64c99fbb9d6..de82e997888 100644
--- a/libstdc++-v3/include/bits/basic_string.tcc
+++ b/libstdc++-v3/include/bits/basic_string.tcc
@@ -171,9 +171,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     basic_string<_CharT, _Traits, _Alloc>::
     _M_create(size_type& __capacity, size_type __old_capacity)
     {
-      _Alloc_result __r = _M_create_plus(__capacity, __old_capacity);
-      __capacity = __r.__count - 1;  // Leave room for NUL.
-      return __r.__ptr;
+      if (__capacity > max_size())
+       std::__throw_length_error(__N("basic_string::_M_create"));
+      if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
+       {
+         __capacity = 2 * __old_capacity;
+         if (__capacity > max_size())
+           __capacity = max_size();
+       }
+      return _S_allocate(_M_get_allocator(), __capacity + 1);
     }
 
   // NB: This is the special case for Input Iterators, used in
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/127228.cc 
b/libstdc++-v3/testsuite/21_strings/basic_string/127228.cc
new file mode 100644
index 00000000000..a0517c320f1
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string/127228.cc
@@ -0,0 +1,25 @@
+// { dg-do run { target c++23 } }
+// { dg-options "-fno-access-control -fno-inline -fno-ipa-sra -fno-ipa-modref 
-fno-ipa-cp -fno-ipa-icf" }
+
+// libstdc++/127228
+// Object files built against older libstdc++ still call _M_create
+// and rely on it to allocate and report allocating no more storage
+// than was asked for when the old capacity is 0.
+
+#include <string>
+#include <testsuite_hooks.h>
+
+int main()
+{
+  std::string s;
+  {
+    std::string::size_type new_cap = 25, old_cap = 0;
+    (void) s._M_create(new_cap, old_cap);
+    VERIFY(new_cap == 25);
+  }
+  {
+    std::string::size_type new_cap = 2, old_cap = 0;
+    (void) s._M_create(new_cap, old_cap);
+    VERIFY(new_cap == 2);
+  }
+}
-- 
2.56.0

Reply via email to