https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109299
Bug ID: 109299 Summary: wrong warning on std::wstring with -O2 -std=c++20 -D_FORTIFY_SOURCE=2 Product: gcc Version: 12.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: benni.buch at gmail dot com Target Milestone: --- I run into this by using googletest with CMake in release mode. ``` #include <string> static std::wstring foo(std::wstring text = {}) { text.resize(42); return text; } int main() { foo(); } ``` ``` $ g++ -O2 -std=c++20 -D_FORTIFY_SOURCE=2 main.cpp In file included from /usr/include/features.h:486, from /usr/include/x86_64-linux-gnu/c++/12/bits/os_defines.h:39, from /usr/include/x86_64-linux-gnu/c++/12/bits/c++config.h:655, from /usr/include/c++/12/string:38, from main.cpp:1: In function ‘wchar_t* wmemcpy(wchar_t*, const wchar_t*, size_t)’, inlined from ‘static constexpr std::char_traits<wchar_t>::char_type* std::char_traits<wchar_t>::copy(char_type*, const char_type*, std::size_t)’ at /usr/include/c++/12/bits/char_traits.h:558:16, inlined from ‘constexpr std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&) [with _CharT = wchar_t; _Traits = std::char_traits<wchar_t>; _Alloc = std::allocator<wchar_t>]’ at /usr/include/c++/12/bits/basic_string.h:675:23, inlined from ‘std::wstring foo(std::wstring)’ at main.cpp:5:12, inlined from ‘int main()’ at main.cpp:9:8: /usr/include/x86_64-linux-gnu/bits/wchar2.h:42:10: warning: call to ‘__wmemcpy_chk_warn’ declared with attribute warning: wmemcpy called with length bigger than size of destination buffer [-Wattribute-warning] 42 | return __glibc_fortify_n (wmemcpy, __n, sizeof (wchar_t), | ^~~~~~~~~~~~~~~~~ ``` On stackoverflow.com you can already find a few discussion about it: - https://stackoverflow.com/questions/75689057 user17732522 reduced it to: ``` #include <wchar.h> wchar_t* _M_dataplus; int _M_string_length; wchar_t _M_local_buf[4]; wchar_t _M_local_buf2[4]; void g(); void f() { g(); _M_string_length = 4; if (_M_dataplus == _M_local_buf) wmemcpy(_M_local_buf2, _M_local_buf, _M_string_length + 1); } ``` ``` $ g++ -O1 -D_FORTIFY_SOURCE=2 -c main.cpp In file included from /usr/include/features.h:486, from /usr/include/x86_64-linux-gnu/bits/libc-header-start.h:33, from /usr/include/wchar.h:27, from main.cpp:1: In function ‘wchar_t* wmemcpy(wchar_t*, const wchar_t*, size_t)’, inlined from ‘void f()’ at main.cpp:15:16: /usr/include/x86_64-linux-gnu/bits/wchar2.h:42:10: warning: call to ‘__wmemcpy_chk_warn’ declared with attribute warning: wmemcpy called with length bigger than size of destination buffer [-Wattribute-warning] 42 | return __glibc_fortify_n (wmemcpy, __n, sizeof (wchar_t), | ^~~~~~~~~~~~~~~~~ ```