PR libstdc++/86169 unshare COW string when non-const data() called
authorJonathan Wakely <jwakely@redhat.com>
Fri, 15 Jun 2018 18:47:29 +0000 (19:47 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Fri, 15 Jun 2018 18:47:29 +0000 (19:47 +0100)
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.

From-SVN: r261642

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/basic_string.h
libstdc++-v3/testsuite/21_strings/basic_string/operations/data/char/86169.cc [new file with mode: 0644]

index b7cef6057b19c664ababfd35838f5a8b2a4fc299..fdb07ab58177992bd2b9cf411b748f8a65c0326e 100644 (file)
@@ -1,5 +1,11 @@
 2018-06-15  Jonathan Wakely  <jwakely@redhat.com>
 
+       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.
+
        * include/std/string_view (basic_string_view(const CharT*)): Remove
        check for null pointer and add nonnull attribute.
        (compare(const CharT*), compare(size_type, size_type, const CharT*))
index fbac38ae9736ae20ff704b9128fc73fbd683580c..577ad5e2f7122ad7e3d97fed927f92e2d2c09c0b 100644 (file)
@@ -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 (file)
index 0000000..92ba287
--- /dev/null
@@ -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);
+}