From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 90DD3385800C; Wed, 5 Jan 2022 16:22:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 90DD3385800C MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-6267] libstdc++: Fix overconstrained std::string constructor [PR103919] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: 3633cc54284450433b81f0340483e15df1a49a3c X-Git-Newrev: 6aa0859afaf28f4fb13121352225bc5877e02a44 Message-Id: <20220105162229.90DD3385800C@sourceware.org> Date: Wed, 5 Jan 2022 16:22:29 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 05 Jan 2022 16:22:29 -0000 https://gcc.gnu.org/g:6aa0859afaf28f4fb13121352225bc5877e02a44 commit r12-6267-g6aa0859afaf28f4fb13121352225bc5877e02a44 Author: Jonathan Wakely Date: Wed Jan 5 15:16:33 2022 +0000 libstdc++: Fix overconstrained std::string constructor [PR103919] The C++17 basic_string(const T&, size_t, size_t) constructor is overconstrained, so it can't be used for a NTBS and a temporary string gets constructed (potentially allocating memory). There is no corresponding constructor taking an NTBS, so no need to disambiguate from it. Accepting an NTBS avoids the temporary (and potential allocation) and is what the standard requires. libstdc++-v3/ChangeLog: PR libstdc++/103919 * include/bits/basic_string.h (basic_string(const T&, size_t, size_t)): Relax constraints on string_view parameter. * include/bits/cow_string.h (basic_string(const T&, size_t, size_t)): Likewise. * testsuite/21_strings/basic_string/cons/char/103919.cc: New test. Diff: --- libstdc++-v3/include/bits/basic_string.h | 3 +- libstdc++-v3/include/bits/cow_string.h | 3 +- .../21_strings/basic_string/cons/char/103919.cc | 43 ++++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index 463cef25b6e..a91ba5114b1 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -766,7 +766,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 * @param __n The number of characters to copy from __t. * @param __a Allocator to use. */ - template> + template>> _GLIBCXX20_CONSTEXPR basic_string(const _Tp& __t, size_type __pos, size_type __n, const _Alloc& __a = _Alloc()) diff --git a/libstdc++-v3/include/bits/cow_string.h b/libstdc++-v3/include/bits/cow_string.h index 8d0b7727be4..84aab2f33c6 100644 --- a/libstdc++-v3/include/bits/cow_string.h +++ b/libstdc++-v3/include/bits/cow_string.h @@ -690,7 +690,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * @param __n The number of characters to copy from __t. * @param __a Allocator to use. */ - template> + template>> basic_string(const _Tp& __t, size_type __pos, size_type __n, const _Alloc& __a = _Alloc()) : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { } diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/cons/char/103919.cc b/libstdc++-v3/testsuite/21_strings/basic_string/cons/char/103919.cc new file mode 100644 index 00000000000..94400e319ff --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string/cons/char/103919.cc @@ -0,0 +1,43 @@ +// { dg-do run { target c++17 } } + +#include +#include +#include +#include +#include + +std::size_t counter = 0; + +void* operator new(std::size_t n) +{ + counter += n; + return std::malloc(n); +} + +void operator delete(void* p) +{ + std::free(p); +} + +void operator delete(void* p, std::size_t) +{ + std::free(p); +} + +int main() +{ + const char* str = "A string that is considerably longer than the SSO buffer"; + + // PR libstdc++/103919 + // basic_string(const T&, size_t, size_t) constructor is overconstrained + counter = 0; + std::string s(str, 2, 6); + VERIFY( s == "string" ); +#if _GLIBCXX_USE_CXX11_ABI + // The string fits in the SSO buffer, so nothing is allocated. + VERIFY( counter == 0 ); +#else + // The COW string allocates a string rep and 7 chars. + VERIFY( counter < std::strlen(str) ); +#endif +}