public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug target/114615] spurious warning on mingw-w64: 'memcpy' reading 4 or more bytes from a region of size 2 with std::wstring{L""} and -flto -O1 [Wstringop-overread]
Date: Mon, 08 Apr 2024 10:54:35 +0000	[thread overview]
Message-ID: <bug-114615-4-ksh16FKeyM@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-114615-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114615

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Or jump threading is splitting the code into two branches for N <= 1 and N >=
2, and then warning that the N >= 2 case would read past the end of the source
buffer. But that case never actually happens.

The constructor calls _M_construct which goes to:

      static void
      _S_copy(_CharT* __d, const _CharT* __s, size_type __n)
      {
        if (__n == 1)
          traits_type::assign(*__d, *__s);
        else
          traits_type::copy(__d, __s, __n);
      }

The N == 1 case is handled here, then char_traits<wchar_t>::copy does:

      static _GLIBCXX20_CONSTEXPR char_type*
      copy(char_type* __s1, const char_type* __s2, size_t __n)
      {
        if (__n == 0)
          return __s1;
#if __cplusplus >= 202002L
        if (std::__is_constant_evaluated())
          return __gnu_cxx::char_traits<char_type>::copy(__s1, __s2, __n);
#endif
        return wmemcpy(__s1, __s2, __n);
      }

So the N == 0 case is also handled here, so we only use wmemcpy for N >= 2. And
that would indeed read N * sizeof(wchar_t), i.e. 4 or more bytes, from L""
which is only 2 bytes.

But it's unreachable, because we take the if (__n == 0) branch.

  parent reply	other threads:[~2024-04-08 10:54 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-05 22:18 [Bug tree-optimization/114615] New: " bugzilla.gcc.simon at arlott dot org
2024-04-05 22:24 ` [Bug target/114615] " pinskia at gcc dot gnu.org
2024-04-08 10:54 ` redi at gcc dot gnu.org [this message]
2024-04-08 10:58 ` redi at gcc dot gnu.org
2024-04-08 21:04 ` pinskia at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-114615-4-ksh16FKeyM@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).