From: Jonathan Wakely Date: Fri, 15 Jun 2018 18:47:29 +0000 (+0100) Subject: PR libstdc++/86169 unshare COW string when non-const data() called X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=525d67d42f0ad365a666c7908e9df7da55aa2f1e;p=gcc.git 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. From-SVN: r261642 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index b7cef6057b1..fdb07ab5817 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,11 @@ 2018-06-15 Jonathan Wakely + 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*)) 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 +// . + +// { 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 +#include + +int main() +{ + const std::string s0{"hello world"}; + std::string s1 {s0}; + char* p = s1.data(); + *p = ' '; + VERIFY(s0.compare("hello world") == 0); +}