From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id D38033858CDB; Wed, 3 Aug 2022 09:29:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D38033858CDB From: "hi at jdoubleu dot de" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/106512] New: String optimization underflows in std::string::operator+ inlining Date: Wed, 03 Aug 2022 09:29:49 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 12.1.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: hi at jdoubleu dot de X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 03 Aug 2022 09:29:49 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D106512 Bug ID: 106512 Summary: String optimization underflows in std::string::operator+ inlining Product: gcc Version: 12.1.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: hi at jdoubleu dot de Target Milestone: --- Live example: https://godbolt.org/z/zMqG8W7WE Given the following code: ```cpp #include std::string GetHello() { return std::string{"ello"}; } int main() { ("H" + GetHello()); } ``` Fails to compile with 1. gcc version 12.1 and newer, 2. linking against gnu++20 and higher 3. all warnings enabled, 4. warnings set to produce an error, 5. -O3 is turned on I get the following error: ``` In file included from /opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/string:40, from :1: In static member function 'static constexpr std::char_traits::char_ty= pe* std::char_traits::copy(char_type*, const char_type*, std::size_t)', inlined from 'static constexpr void std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::_S_copy(_CharT*, const _CharT*, size_type) [with _CharT = =3D char; _Traits =3D std::char_traits; _Alloc =3D std::allocator]'= at /opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/bits/basic_string.h:42= 3:21, inlined from 'constexpr std::__cxx11::basic_string<_CharT, _Traits, _Allocator>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::_M_replace(size_type, size_type, const _CharT*, size_type) [with _CharT =3D char; _Traits =3D std::char_traits; _Alloc =3D std::allocator]' at /opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/bits/basic_string.tcc:= 532:22, inlined from 'constexpr std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::replace(size_type, size_type, const _CharT*, size_type) [with _Cha= rT =3D char; _Traits =3D std::char_traits; _Alloc =3D std::allocator]'= at /opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/bits/basic_string.h:21= 71:19, inlined from 'constexpr std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::insert(size_t= ype, const _CharT*) [with _CharT =3D char; _Traits =3D std::char_traits; _= Alloc =3D std::allocator]' at /opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/bits/basic_string.h:19= 28:22, inlined from 'constexpr std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const _CharT*, __cxx11::basic_string<_CharT, _Traits, _Allocator>&&) [with _CharT =3D char; _Traits =3D char_traits; _Alloc =3D allocator]' at /opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/bits/basic_string.h:35= 41:36, inlined from 'int main()' at :10:10: /opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/bits/char_traits.h:431= :56: error: 'void* __builtin_memcpy(void*, const void*, long unsigned int)' accessing 9223372036854775810 or more bytes at offsets [18, 9223372036854775807] and 17 may overlap up to 9223372036854775813 bytes at offset -3 [-Werror=3Drestrict] 431 | return static_cast(__builtin_memcpy(__s1, __s2, __n)); |=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~ ``` I'm not sure what the issue is here exactly. From the error message, it loo= ks some underflow (of `long long`) when trying to inline the std::string::operator+? It doesn't seem like a bug in libstdc++, since it compiles with gcc11. Furthermore, if you just change the `"H" + ...` in the example to `"He" + .= ..` it suddenly works. The symptoms of this one look similar: https://gcc.gnu.org/bugzilla//show_bug.cgi?id=3D85651=