Bug 82944 - missing -Wstringop-truncation on strncpy due to system header macro
Summary: missing -Wstringop-truncation on strncpy due to system header macro
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 8.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: Wstringop-truncation
  Show dependency treegraph
 
Reported: 2017-11-10 18:09 UTC by Martin Sebor
Modified: 2021-11-05 23:18 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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.
Comment 2 Tim Turner 2021-11-05 23:18:52 UTC Comment hidden (spam)