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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
In the call to strncpy the warning is suppressed because it's a common use case
to then add the nul byte (the suppression also only looks forward but not
backward).

Calling strncat like in the test program isn't a common use case so the warning
just points out that the copy truncates the string.  This isn't a bug but a
design choice, analogous to -Wstringop-truncation warning about the same
problem in:

$ cat a.c && gcc -O2 -S -Wall a.c
char a[4];

void f (void)
{
  char s[] = "test";
  __builtin_snprintf (a, 3, "%s", s);
}
a.c: In function ‘f’:
a.c:6:30: warning: ‘%s’ directive output truncated writing 4 bytes into a
region of size 3 [-Wformat-truncation=]
    6 |   __builtin_snprintf (a, 3, "%s", s);
      |                              ^~   ~
a.c:6:3: note: ‘__builtin_snprintf’ output 5 bytes into a destination of size 3
    6 |   __builtin_snprintf (a, 3, "%s", s);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Reply via email to