aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2018-06-15 19:19:04 +0000
committerJonathan Wakely <jwakely@redhat.com>2018-06-15 19:19:04 +0000
commit39c003c506afbf9a2a8067616f98f4341f0992d3 (patch)
treee65e5fc6113682e3d8f66c71a0fb4f637a738bd4
parente8d64a7e59c278d05fd2c14668ea36e48fd42769 (diff)
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. git-svn-id: https://gcc.gnu.org/svn/gcc/branches/gcc-7-branch@261646 138bc75d-0d04-0410-961f-82ee72b054a4
-rw-r--r--libstdc++-v3/ChangeLog8
-rw-r--r--libstdc++-v3/include/bits/basic_string.h5
-rw-r--r--libstdc++-v3/testsuite/21_strings/basic_string/operations/data/char/86169.cc37
3 files changed, 49 insertions, 1 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog
index af164a37e96..2634664576c 100644
--- a/libstdc++-v3/ChangeLog
+++ b/libstdc++-v3/ChangeLog
@@ -1,5 +1,13 @@
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
define for C++17 and above.
diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h
index 7df39685ad3..674fe0963eb 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..a8b0ff4aa3f
--- /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++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);
+}