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

            Bug ID: 114374
           Summary: [12/13 Regression] snprintf Wformat-truncation
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: torsten.mandel at sap dot com
  Target Milestone: ---

Source:

// ===========================================
#include "stdio.h"

int main() {
    char buf[11];
    int loop(1);
    do {
        snprintf(buf,sizeof(buf),"%d",loop++);
    } while(true);
}
// ===========================================


Compiled with: gcc -Wall

triggers:

./buffertest.cpp: In function ‘int main()’:
./buffertest.cpp:8:37: warning: ‘snprintf’ output may be truncated before the
last format character [-Wformat-truncation=]
    8 |         snprintf(buf,sizeof(buf),"%d",loop++);
      |                                     ^
./buffertest.cpp:8:17: note: ‘snprintf’ output between 2 and 12 bytes into a
destination of size 11
    8 |         snprintf(buf,sizeof(buf),"%d",loop++);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

when using gcc-12 and gcc-13; gcc-11 compiled without error.

Replacing loop++ with ++loop does not trigger the error; neither case should be
an error as the overflow is UB in both cases and only positive numbers should
be considered regarding the target buffer size.

Reply via email to