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
          Reporter: msebor at gcc dot
  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
   __builtin_strncpy (a, s, sizeof a);   // -Wstringop-truncation (good)

Reply via email to