]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
PR libstdc++/86169 unshare COW string when non-const data() called
authorJonathan Wakely <jwakely@redhat.com>
Fri, 15 Jun 2018 19:19:04 +0000 (20:19 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Fri, 15 Jun 2018 19:19:04 +0000 (20:19 +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: r261646

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 af164a37e964940f080d1ac8bd57b58f2ffd5943..2634664576cf860a638eca308b4e73a7ff4d53b3 100644 (file)
@@ -1,3 +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.
+
 2018-06-15  Jonathan Wakely  <jwakely@redhat.com>
 
        * include/bits/char_traits.h (__cpp_lib_constexpr_char_traits): Only
index 7df39685ad3305320f0720074f6cdd07a7681433..674fe0963eb1aae80b58faf84ce72c01eae4f073 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..a8b0ff4
--- /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++1z } }
+
+// 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);
+}