[Bug c/84649] New: -Wstringop-truncation shouldn't warn on strncat() when 2nd argument is a char array

sirl at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Thu Mar 1 15:04:00 GMT 2018


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

            Bug ID: 84649
           Summary: -Wstringop-truncation shouldn't warn on strncat() when
                    2nd argument is a char array
           Product: gcc
           Version: 8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: sirl at gcc dot gnu.org
  Target Milestone: ---

With gcc-8 trunk@258093 for this example

char *append_leading_digits(char *cp, int i)
{
  char buf[16];
  __builtin_sprintf(buf, "%2i ", i);
  __builtin_strncat(cp, buf, 4);
  return cp;
}

gcc warns like that:

gcc-trunk -O2 -c test-strncat.c -Wstringop-truncation
test-strncat.c: In function 'append_leading_digits':
test-strncat.c:8:2: warning: '__builtin_strncat' output may be truncated
copying 3 bytes from a string of length 15 [-Wstringop-truncation]
  __builtin_strncat(cp, buf, 4);
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I believe this warning is unjustified as buf[] may contain strings of varying
lengths and the whole purpose of strncat() is to truncate the source string
after all.
Actually maybe the warning should only trigger for strncat() when BOTH the 2nd
and 3rd argument are constant (like in the example in the manual)?


More information about the Gcc-bugs mailing list