Compiling using -D_GLIBCXX_ASSERTIONS fails in gcc 12.1.0, worked in 11.1.0: #include <string> extern std::string a(); std::string a() { std::string x; x.assign( "Z" ); return x; } using command line: g++ -m64 -c -std=gnu++17 -Wall -O3 -Werror -D_GLIBCXX_ASSERTIONS x.cpp fails with: In file included from /usr/local/gcc-12.1.0/include/c++/12.1.0/string:40, from x.cpp:14: In static member function ??static std::char_traits<char>::char_type* std::char_traits<char>::copy(char_type*, const char_type*, std::size_t)??, inlined from ??static void std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::_S_copy(_CharT*, const _CharT*, size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]?? at /usr/local/gcc-12.1.0/include/c++/12.1.0/bits/basic_string.h:423:21, inlined from ??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 = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]?? at /usr/local/gcc-12.1.0/include/c++/12.1.0/bits/basic_string.tcc:532:22, inlined from ??std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::assign(const _CharT*) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]?? at /usr/local/gcc-12.1.0/include/c++/12.1.0/bits/basic_string.h:1647:19, inlined from ??std::string a()?? at x.cpp:22:12: /usr/local/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 [2, 9223372036854775807] and 17 may overlap up to 9223372036854775813 bytes at offset -3 [-Werror=restrict] 431 | return static_cast<char_type*>(__builtin_memcpy(__s1, __s2, __n)); | ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~ cc1plus: all warnings being treated as errors
Started with r12-3347-g8af8abfbbace49e6.
(In reply to John Buddery from comment #0) > g++ -m64 -c -std=gnu++17 -Wall -O3 -Werror -D_GLIBCXX_ASSERTIONS x.cpp This is only a warning. It fails because you asked it to, with -Werror. Reporting that compilation fails when you turn warnings into errors isn't very interesting, that's the point of -Werror :-) So the bug here is a bogus warning, not that compilation fails. Summary adjusted accordingly.
I don't think _GLIBCXX_ASSERTIONS is necessary; you just need -Werror=restrict and -O2 (not sure what exactly). https://godbolt.org/z/TvfYzbcf6
You don't need -D_GLIBCXX_ASSERTIONS in C++20 mode but you do in C++17 mode it seems.
On top of -O1 you seem to need all of -fexpensive-optimizations -ftree-vrp -fipa-sra to trigger it.
The reason it only happens with -D_GLIBCXX_ASSERTIONS or in C++20 mode is that both of those stop the use of the explicit instantiations for basic_string and cause them to be implicitly instantiated.
FWIW, I'm getting this warning in one of the Boost.Describe examples (https://godbolt.org/z/WKMjeTdne) from innocent-looking code that concatenates std::strings with op+.
GCC 12.2 is being released, retargeting bugs to GCC 12.3.
My Godbolt link above no longer reproduces the warning because of https://github.com/boostorg/describe/commit/c8c46bfdf78022a8a7e9e06983d8b04ccb921991, but this one does: https://godbolt.org/z/oT1M31osa. Looks like trunk has fixed the issue, though: https://godbolt.org/z/1GGvYWxKG.
I can confirm this works on the gcc-13 branch, for both c++17 and c++20 - feel free to close this issue.
It was fixed by r13-2618-g723ef5a937dbab so let's close this as a dup of PR 105651 *** This bug has been marked as a duplicate of bug 105651 ***