Bug 82944

Summary: missing -Wstringop-truncation on strncpy due to system header macro
Product: gcc Reporter: Martin Sebor <msebor>
Component: tree-optimizationAssignee: Not yet assigned to anyone <unassigned>
Status: UNCONFIRMED ---    
Severity: normal CC: dimhen
Priority: P3 Keywords: diagnostic
Version: 8.0   
Target Milestone: ---   
See Also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81117
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83412
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:
Bug Depends on:    
Bug Blocks: 88781    

Description Martin Sebor 2017-11-10 18:09:08 UTC
The -Wstringop-truncation warning added in GCC 8.0 via r254630 for bug 81117 is specifically intended to highlight likely unintended uses of the strncpy function that truncate the terminating NUL charcter from the source string.  An example of such a misuse given in the request is the following:

  char buf[2];

  void test (const char* str)
  {
    strncpy (buf, str, strlen (str));
  }

As it turns out, although the new checker works correctly when strncpy is declared as a function, when it's defined as a shadow macro in a system header (as happens to be the case in in Glibc 2.24 and prior) the warning is suppressed:

$ (set -x && cat pr81117.c && for opt in '' '--include=string.h'; do gcc -O2 -S -Wall -Wextra $opt pr81117.c; done)
+ cat pr81117.c
extern __SIZE_TYPE__ strlen (const char*);
extern char* strncpy (char*, const char*, __SIZE_TYPE__);

char buf[2];

void test (const char* str)
{
  strncpy (buf, str, strlen (str));
}
+ for opt in ''\'''\''' ''\''--include=string.h'\'''
+ /ssd/build/gcc-svn/gcc/xgcc -B /ssd/build/gcc-svn/gcc -O2 -S -Wall -Wextra pr81117.c
pr81117.c: In function ‘test’:
pr81117.c:8:3: warning: ‘strncpy’ output truncated before terminating nul copying as many bytes from a string as its length [-Wstringop-truncation]
   strncpy (buf, str, strlen (str));
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ for opt in ''\'''\''' ''\''--include=string.h'\'''
+ /ssd/build/gcc-svn/gcc/xgcc -B /ssd/build/gcc-svn/gcc -O2 -S -Wall -Wextra --include=string.h pr81117.c
Comment 1 Martin Sebor 2017-12-13 17:32:19 UTC
I wonder if this is caused by the same underlying bug as pr83412.