Bug 105545 - [12/13/14 Regression] Warning for string assignment with _GLIBCXX_ASSERTIONS since r12-3347-g8af8abfbbace49e6
Summary: [12/13/14 Regression] Warning for string assignment with _GLIBCXX_ASSERTIONS ...
Status: RESOLVED DUPLICATE of bug 105651
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 12.1.0
: P2 normal
Target Milestone: 12.3
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2022-05-10 11:15 UTC by John Buddery
Modified: 2023-04-20 12:00 UTC (History)
8 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2022-05-10 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description John Buddery 2022-05-10 11:15:33 UTC
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
Comment 1 Martin Liška 2022-05-10 11:18:19 UTC
Started with r12-3347-g8af8abfbbace49e6.
Comment 2 Jonathan Wakely 2022-05-10 12:26:00 UTC
(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.
Comment 3 Ed Catmur 2022-05-12 15:17:57 UTC
I don't think _GLIBCXX_ASSERTIONS is necessary; you just need -Werror=restrict and -O2 (not sure what exactly). https://godbolt.org/z/TvfYzbcf6
Comment 4 Tom Hughes 2022-05-12 15:51:27 UTC
You don't need -D_GLIBCXX_ASSERTIONS in C++20 mode but you do in C++17 mode it seems.
Comment 5 Tom Hughes 2022-05-12 16:00:48 UTC
On top of -O1 you seem to need all of -fexpensive-optimizations -ftree-vrp -fipa-sra to trigger it.
Comment 6 Tom Hughes 2022-05-31 07:33:42 UTC
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.
Comment 7 Peter Dimov 2022-06-21 15:35:24 UTC
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+.
Comment 8 Richard Biener 2022-08-19 08:26:19 UTC
GCC 12.2 is being released, retargeting bugs to GCC 12.3.
Comment 9 Peter Dimov 2022-11-03 18:07:50 UTC
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.
Comment 10 John Buddery 2023-04-17 16:39:51 UTC
I can confirm this works on the gcc-13 branch, for both c++17 and c++20 - feel free to close this issue.
Comment 11 Jonathan Wakely 2023-04-20 12:00:25 UTC
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 ***