public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/109299] New: wrong warning on std::wstring with -O2 -std=c++20 -D_FORTIFY_SOURCE=2
@ 2023-03-27 15:01 benni.buch at gmail dot com
  2023-03-27 15:38 ` [Bug tree-optimization/109299] " xry111 at gcc dot gnu.org
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: benni.buch at gmail dot com @ 2023-03-27 15:01 UTC (permalink / raw)
  To: gcc-bugs

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),
      |          ^~~~~~~~~~~~~~~~~
```

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2023-03-28 23:34 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-27 15:01 [Bug libstdc++/109299] New: wrong warning on std::wstring with -O2 -std=c++20 -D_FORTIFY_SOURCE=2 benni.buch at gmail dot com
2023-03-27 15:38 ` [Bug tree-optimization/109299] " xry111 at gcc dot gnu.org
2023-03-27 20:57 ` redi at gcc dot gnu.org
2023-03-27 21:14 ` benni.buch at gmail dot com
2023-03-28  7:11 ` [Bug libstdc++/109299] [12/13 Regression] " rguenth at gcc dot gnu.org
2023-03-28  9:28 ` redi at gcc dot gnu.org
2023-03-28  9:37 ` redi at gcc dot gnu.org
2023-03-28  9:47 ` jakub at gcc dot gnu.org
2023-03-28 10:03 ` redi at gcc dot gnu.org
2023-03-28 10:05 ` redi at gcc dot gnu.org
2023-03-28 20:14 ` cvs-commit at gcc dot gnu.org
2023-03-28 23:33 ` [Bug libstdc++/109299] [12 " cvs-commit at gcc dot gnu.org
2023-03-28 23:34 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).