[Bug middle-end/106559] New: Spurious warning format-truncation (regression from 9)

phdiv at fastmail dot fm gcc-bugzilla@gcc.gnu.org
Mon Aug 8 15:10:58 GMT 2022


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

            Bug ID: 106559
           Summary: Spurious warning format-truncation (regression from 9)
           Product: gcc
           Version: 10.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: phdiv at fastmail dot fm
  Target Milestone: ---

Created attachment 53426
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53426&action=edit
Reproducer.

> cat w.cpp
extern char buffer[64];
extern unsigned int number[16];

void f(){
  static const char string[16][3]={
      "01","02","03","04","05","06","07","08",
      "09","10","11","12","13","14","15","16"};

  for(unsigned int i=0; i<16; ++i)
    __builtin_snprintf(buffer,sizeof(buffer),"%u (%s):   %8x",
      i,string[i],number[i]);
}

> g++ -O1 -Wformat-truncation -c w.cpp
w.cpp: In function 'void f()':
w.cpp:10:61: warning: '__builtin_snprintf' output may be truncated before the
last format character [-Wformat-truncation=]
   10 |     __builtin_snprintf(buffer,sizeof(buffer),"%u (%s):   %8x",
      |                                                             ^
w.cpp:10:23: note: '__builtin_snprintf' output between 17 and 65 bytes into a
destination of size 64
   10 |     __builtin_snprintf(buffer,sizeof(buffer),"%u (%s):   %8x",
      |     ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   11 |       i,string[i],number[i]);
      |       ~~~~~~~~~~~~~~~~~~~~~~


Granted, the documentation of Wformat-truncation mentions possible false
positives, but this is a regression in GCC 10 with respect to GCC 9
(and present up to trunk).

For some more variations which do or do not exhibit this bug, see 
https://godbolt.org/z/PvfrchP7E .


More information about the Gcc-bugs mailing list