From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 7248C3861828; Thu, 11 Jan 2024 23:16:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7248C3861828 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1705014983; bh=7lyZBAwrQyhUV7W/X+ztpT8aKqMTd/mLIeJrOIRG8+I=; h=From:To:Subject:Date:From; b=UWRGdY+VihJ+1KrEv0CIKctj107Bq2S3r5GzLPEStRUfPqGsL9ihqXXk1l4qz0cPB F3uwbHLmaa9rkNjCb4N/TzG6fSKiqwQFNlvS0JhiXmb6Xrxx80J5gkKRS+/mlb/9qq u9V84Nl3BNJtPgpPU0saxz81ObQlbj14KTEV+TRE= 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-10090] libstdc++: Fix std::char_traits::move [PR113200] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: da19967df3ad5d123888ef24b4fd84be047df226 X-Git-Newrev: 26a9e8cee4d20e5b08c0336439c8f69a2f06af1c Message-Id: <20240111231623.7248C3861828@sourceware.org> Date: Thu, 11 Jan 2024 23:16:23 +0000 (GMT) List-Id: https://gcc.gnu.org/g:26a9e8cee4d20e5b08c0336439c8f69a2f06af1c commit r12-10090-g26a9e8cee4d20e5b08c0336439c8f69a2f06af1c Author: Jonathan Wakely Date: Wed Jan 3 15:01:09 2024 +0000 libstdc++: Fix std::char_traits::move [PR113200] The current constexpr implementation of std::char_traits::move relies on being able to compare the pointer parameters, which is not allowed for unrelated pointers. We can use __builtin_constant_p to determine whether it's safe to compare the pointers directly. If not, then we know the ranges must be disjoint and so we can use char_traits::copy to copy forwards from the first character to the last. If the pointers can be compared directly, then we can simplify the condition for copying backwards to just two pointer comparisons. libstdc++-v3/ChangeLog: PR libstdc++/113200 * include/bits/char_traits.h (__gnu_cxx::char_traits::move): Use __builtin_constant_p to check for unrelated pointers that cannot be compared during constant evaluation. * testsuite/21_strings/char_traits/requirements/113200.cc: New test. (cherry picked from commit 15cc291887dc9dd92b2c93f4545e20eb6c190122) Diff: --- libstdc++-v3/include/bits/char_traits.h | 16 +++------------- .../21_strings/char_traits/requirements/113200.cc | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/libstdc++-v3/include/bits/char_traits.h b/libstdc++-v3/include/bits/char_traits.h index 3c136f930ef..a3a5bbfb643 100644 --- a/libstdc++-v3/include/bits/char_traits.h +++ b/libstdc++-v3/include/bits/char_traits.h @@ -210,8 +210,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #if __cplusplus >= 202002L if (std::__is_constant_evaluated()) { - if (__s1 == __s2) // unlikely, but saves a lot of work - return __s1; #if __cpp_constexpr_dynamic_alloc // The overlap detection below fails due to PR c++/89074, // so use a temporary buffer instead. @@ -220,17 +218,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION copy(__s1, __tmp, __n); delete[] __tmp; #else - const auto __end = __s2 + __n - 1; - bool __overlap = false; - for (std::size_t __i = 0; __i < __n - 1; ++__i) - { - if (__s1 + __i == __end) - { - __overlap = true; - break; - } - } - if (__overlap) + // Use __builtin_constant_p to avoid comparing unrelated pointers. + if (__builtin_constant_p(__s2 < __s1) + && __s1 > __s2 && __s1 < (__s2 + __n)) { do { diff --git a/libstdc++-v3/testsuite/21_strings/char_traits/requirements/113200.cc b/libstdc++-v3/testsuite/21_strings/char_traits/requirements/113200.cc new file mode 100644 index 00000000000..0fe765d53bc --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/char_traits/requirements/113200.cc @@ -0,0 +1,20 @@ +// { dg-do compile { target c++20 } } + +// PR libstdc++/113200 +// char_traits::move is not constexpr when the argument is a string literal + +#include + +template struct S +{ + char data_[ N ]; + + constexpr S( char const* p ): data_{} + { + std::char_traits::move( data_, p, N ); + } +}; + +template S( char const(&)[N] ) -> S; + +constexpr S s( "test" );