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

            Bug ID: 82652
           Summary: missing -Wstringop-overflow on strncpy with
                    -fcheck-pointer-bounds
           Product: gcc
           Version: 8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

When compiling without -fcheck-pointer-bounds the buffer overflow in all of the
calls below are diagnosed.  But when using -fcheck-pointer-bounds -mmpx none of
them is.

$ cat a.c && gcc -O2 -S -Wall -fcheck-pointer-bounds -mmpx a.c
extern char* stpncpy (char*, const char*, __SIZE_TYPE__);
extern char* strncpy (char*, const char*, __SIZE_TYPE__);

#define S "01234567"

char a[4];

char* f0 (void)
{
  return strncpy (a, S, sizeof S);   // missing -Wstringop-overflow
}

char* f1 (void)
{
  char b[sizeof S];
  __builtin_strcpy (b, S);

  return strncpy (a, b, sizeof b);   // missing -Wstringop-overflow
}

char* g0 (void)
{
  return stpncpy (a, S, sizeof S);   // missing -Wstringop-overflow
}

char* g1 (void)
{
  char b[sizeof S];
  __builtin_strcpy (b, S);

  return stpncpy (a, b, sizeof b);   // missing -Wstringop-overflow
}


The expected output is something like this (from the trunk of GCC 8.0; GCC 7.2
only diagnoses f0 and f1):

a.c: In function ‘f0’:
a.c:10:10: warning: ‘__builtin_memcpy’ writing 9 bytes into a region of size 4
overflows the destination [-Wstringop-overflow=]
   return strncpy (a, S, sizeof S);
          ^~~~~~~~~~~~~~~~~~~~~~~~
a.c: In function ‘f1’:
a.c:18:10: warning: ‘strncpy’ writing 9 bytes into a region of size 4 overflows
the destination [-Wstringop-overflow=]
   return strncpy (a, b, sizeof b);
          ^~~~~~~~~~~~~~~~~~~~~~~~
a.c: In function ‘g0’:
a.c:23:10: warning: ‘stpncpy’ writing 9 bytes into a region of size 4 overflows
the destination [-Wstringop-overflow=]
   return stpncpy (a, S, sizeof S);
          ^~~~~~~~~~~~~~~~~~~~~~~~
a.c: In function ‘g1’:
a.c:31:10: warning: ‘stpncpy’ writing 9 bytes into a region of size 4 overflows
the destination [-Wstringop-overflow=]
   return stpncpy (a, b, sizeof b);
          ^~~~~~~~~~~~~~~~~~~~~~~~

Reply via email to