[Bug tree-optimization/84396] New: missing -Wstringop-truncation with non-nul assignment to destination

msebor at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Wed Feb 14 23:39:00 GMT 2018


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

            Bug ID: 84396
           Summary: missing -Wstringop-truncation with non-nul assignment
                    to destination
           Product: gcc
           Version: 8.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: ---

As pointed out in bug 84383 comment #2, a comment in the -Wstringop-truncation
checker suggests that the warning should be suppressed by the assignment of
'\0'  to the destination after a potentially truncating call to strncpy. 
However, an inspection of the code as well as the test case below indicate that
assigning even a non-NUL value to the destination suppresses the warning.

$ cat t.c && gcc -O2 -S -Wall -Wextra t.c
char a[7];

void f (const char *s)
{
  __builtin_strncpy (a, s, sizeof a);   // -Wstringop-truncation (good)
}

void g (const char *s)
{
  __builtin_strncpy (a, s, sizeof a);   // no -Wstringop-truncation (good)
  a[sizeof a - 1] = 0;
}

void h (const char *s)
{
  __builtin_strncpy (a, s, sizeof a);   // missing -Wstringop-truncation
  a[sizeof a - 1] = '\n';
}

t.c: In function ‘f’:
t.c:5:3: warning: ‘__builtin_strncpy’ specified bound 7 equals destination size
[-Wstringop-truncation]
   __builtin_strncpy (a, s, sizeof a);   // -Wstringop-truncation (good)
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


More information about the Gcc-bugs mailing list