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

            Bug ID: 93646
           Summary: confusing -Wstringop-truncation on strncat where
                    -Wstringop-overflow is expected
           Product: gcc
           Version: 10.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: ---

The -Wstringop-overflow warning in the code below is expected because a call to
strncat with a bound equal to the length of the source might overflow (the
bound should be equal to the size of the space remaining in the destination). 
But -Wstringop-truncation is not expected because strncat a) never copies the
terminating nul from the source and b) always appends a terminating nul to the
destination.

$ cat z.c && gcc -O2 -S -Wall z.c
void f (char *d, const char *s)
{
  __builtin_strncat (d, s, __builtin_strlen (s));
}

char a[4];

void g (void)
{
  f (a, "123");
}
z.c: In function ‘f’:
z.c:3:3: warning: ‘__builtin_strncat’ output truncated before terminating nul
copying as many bytes from a string as its length [-Wstringop-truncation]
    3 |   __builtin_strncat (d, s, __builtin_strlen (s));
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In function ‘f’,
    inlined from ‘g’ at z.c:10:3:
z.c:3:3: warning: ‘__builtin_strncat’ specified bound 3 equals source length
[-Wstringop-overflow=]
    3 |   __builtin_strncat (d, s, __builtin_strlen (s));
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Reply via email to