From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 6A7E13858430 for ; Wed, 5 Jan 2022 16:22:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6A7E13858430 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-348-_nAk_8FrNkW2iHY8DTpSbg-1; Wed, 05 Jan 2022 11:22:49 -0500 X-MC-Unique: _nAk_8FrNkW2iHY8DTpSbg-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4FAE080A5FA; Wed, 5 Jan 2022 16:22:47 +0000 (UTC) Received: from localhost (unknown [10.33.36.252]) by smtp.corp.redhat.com (Postfix) with ESMTP id EFAC08A2E4; Wed, 5 Jan 2022 16:22:46 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix overconstrained std::string constructor [PR103919] Date: Wed, 5 Jan 2022 16:22:45 +0000 Message-Id: <20220105162245.1779686-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII" X-Spam-Status: No, score=-13.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, URI_HEX autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 05 Jan 2022 16:22:52 -0000 Tested powerpc64le-linux, pushed to trunk. 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. --- libstdc++-v3/include/bits/basic_string.h | 3 +- libstdc++-v3/include/bits/cow_string.h | 3 +- .../basic_string/cons/char/103919.cc | 43 +++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 libstdc++-v3/testsuite/21_strings/basic_string/cons/char/103919.cc 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 +} -- 2.31.1