When I added the non-const basic_string::data() overload I made a
silly mistake in the COW version, failing to "leak" the string (i.e.
unshare it and return a pointer to a uniquely-owned string).

        PR libstdc++/86169
        * include/bits/basic_string.h [!_GLIBCXX_USE_CXX11_ABI]
        (basic_string::data()): Unshare string.
        * testsuite/21_strings/basic_string/operations/data/char/86169.cc:
        New.

Tested powerpc64le-linux, committed to trunk. Backports to 7 and 8
coming soon.

commit 2a2324302cffa057ff7b2fa1b8ccec4fe23087ef
Author: Jonathan Wakely <jwak...@redhat.com>
Date:   Fri Jun 15 18:22:37 2018 +0100

    PR libstdc++/86169 unshare COW string when non-const data() called
    
            PR libstdc++/86169
            * include/bits/basic_string.h [!_GLIBCXX_USE_CXX11_ABI]
            (basic_string::data()): Unshare string.
            * testsuite/21_strings/basic_string/operations/data/char/86169.cc:
            New.

diff --git a/libstdc++-v3/include/bits/basic_string.h 
b/libstdc++-v3/include/bits/basic_string.h
index fbac38ae973..577ad5e2f71 100644
--- a/libstdc++-v3/include/bits/basic_string.h
+++ b/libstdc++-v3/include/bits/basic_string.h
@@ -5130,7 +5130,10 @@ _GLIBCXX_END_NAMESPACE_CXX11
       */
       _CharT*
       data() noexcept
-      { return _M_data(); }
+      {
+       _M_leak();
+       return _M_data();
+      }
 #endif
 
       /**
diff --git 
a/libstdc++-v3/testsuite/21_strings/basic_string/operations/data/char/86169.cc 
b/libstdc++-v3/testsuite/21_strings/basic_string/operations/data/char/86169.cc
new file mode 100644
index 00000000000..92ba287aefe
--- /dev/null
+++ 
b/libstdc++-v3/testsuite/21_strings/basic_string/operations/data/char/86169.cc
@@ -0,0 +1,37 @@
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++17" }
+// { dg-do run { target c++17 } }
+
+// PR libstdc++/86169
+
+#ifndef _GLIBCXX_USE_CXX11_ABI
+# define _GLIBCXX_USE_CXX11_ABI 0
+#endif
+
+#include <string>
+#include <testsuite_hooks.h>
+
+int main()
+{
+  const std::string s0{"hello world"};
+  std::string s1 {s0};
+  char* p = s1.data();
+  *p = ' ';
+  VERIFY(s0.compare("hello world") == 0);
+}

Reply via email to