public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/97631] New: bogus "writing one too many bytes" warning for memcpy with strlen argument
@ 2020-10-29 16:16 msebor at gcc dot gnu.org
  2020-10-29 16:17 ` [Bug tree-optimization/97631] [10/11 Regression] " msebor at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: msebor at gcc dot gnu.org @ 2020-10-29 16:16 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 97631
           Summary: bogus "writing one too many bytes" warning for memcpy
                    with strlen argument
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

The -Wstringop-overflow instance in f() is correct and intended but the one in
g() is neither.  It most likely crept in by mistake in r279392.  The test case
was reduced from libfido2.

$ cat xxx.c && gcc -O2 -S -Wall xxx.c
typedef __SIZE_TYPE__ size_t;

char* f (char *s)
{
  size_t n = __builtin_strlen (s);
  if (n == 0)
    return 0;

  char *d = __builtin_malloc (n);
  __builtin_strcpy (d, s);       // -Wstringop-overflow (good)
  return d;
}

char* g (char *s)
{
  size_t n = __builtin_strlen (s);
  if (n == 0)
    return 0;

  char *d = __builtin_malloc (n);
  __builtin_memcpy (d, s, n);    // bogus overflow warning
  return d;
}

xxx.c: In function ‘f’:
xxx.c:10:3: warning: ‘__builtin_strcpy’ writing one too many bytes into a
region of a size that depends on ‘strlen’ [-Wstringop-overflow=]
   10 |   __builtin_strcpy (d, s);       // -Wstringop-overflow (good)
      |   ^~~~~~~~~~~~~~~~~~~~~~~
xxx.c:9:13: note: at offset 0 to an object allocated by ‘__builtin_malloc’ here
    9 |   char *d = __builtin_malloc (n);
      |             ^~~~~~~~~~~~~~~~~~~~
xxx.c: In function ‘g’:
xxx.c:21:3: warning: ‘__builtin_memcpy’ writing one too many bytes into a
region of a size that depends on ‘strlen’ [-Wstringop-overflow=]
   21 |   __builtin_memcpy (d, s, n);    // bogus overflow warning
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~
xxx.c:20:13: note: at offset 0 to an object allocated by ‘__builtin_malloc’
here
   20 |   char *d = __builtin_malloc (n);
      |             ^~~~~~~~~~~~~~~~~~~~

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

end of thread, other threads:[~2023-07-07  9:11 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-29 16:16 [Bug tree-optimization/97631] New: bogus "writing one too many bytes" warning for memcpy with strlen argument msebor at gcc dot gnu.org
2020-10-29 16:17 ` [Bug tree-optimization/97631] [10/11 Regression] " msebor at gcc dot gnu.org
2020-10-29 16:25 ` msebor at gcc dot gnu.org
2020-10-30  8:26 ` rguenth at gcc dot gnu.org
2021-02-18 22:31 ` msebor at gcc dot gnu.org
2021-03-08 20:31 ` cvs-commit at gcc dot gnu.org
2021-03-08 20:32 ` [Bug tree-optimization/97631] [10 " msebor at gcc dot gnu.org
2021-04-08 12:02 ` rguenth at gcc dot gnu.org
2022-03-17 19:44 ` msebor at gcc dot gnu.org
2022-06-28 10:42 ` jakub at gcc dot gnu.org
2023-07-07  9:11 ` rguenth 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).