public inbox for libstdc++-cvs@sourceware.org help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r11-9435] libstdc++: Fix overconstrained std::string constructor [PR103919] Date: Wed, 5 Jan 2022 22:06:41 +0000 (GMT) [thread overview] Message-ID: <20220105220641.EBFFF3858425@sourceware.org> (raw) https://gcc.gnu.org/g:4c64143f32642d22590959704e2ec6c686d745ff commit r11-9435-g4c64143f32642d22590959704e2ec6c686d745ff Author: Jonathan Wakely <jwakely@redhat.com> 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. [!_GLIBCXX_USE_CXX11_ABI] (basic_string(const T&, size_t, size_t)): Likewise. * testsuite/21_strings/basic_string/cons/char/103919.cc: New test. (cherry picked from commit 6aa0859afaf28f4fb13121352225bc5877e02a44) Diff: --- libstdc++-v3/include/bits/basic_string.h | 6 ++- .../21_strings/basic_string/cons/char/103919.cc | 43 ++++++++++++++++++++++ 2 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 5d09f774e03..e6fa6eb2354 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -648,7 +648,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 * @param __n The number of characters to copy from __t. * @param __a Allocator to use. */ - template<typename _Tp, typename = _If_sv<_Tp, void>> + template<typename _Tp, + typename = enable_if_t<is_convertible_v<const _Tp&, __sv_type>>> 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) { } @@ -3743,7 +3744,8 @@ _GLIBCXX_END_NAMESPACE_CXX11 * @param __n The number of characters to copy from __t. * @param __a Allocator to use. */ - template<typename _Tp, typename = _If_sv<_Tp, void>> + template<typename _Tp, + typename = enable_if_t<is_convertible_v<const _Tp&, __sv_type>>> 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 <string> +#include <new> +#include <cstdlib> +#include <cstring> +#include <testsuite_hooks.h> + +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 +}
reply other threads:[~2022-01-05 22:06 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20220105220641.EBFFF3858425@sourceware.org \ --to=redi@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ --cc=libstdc++-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).