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

            Bug ID: 82944
           Summary: missing -Wstringop-truncation on strncpy due to system
                    header macro
           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: ---

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

Reply via email to