public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/114374] New: [12/13 Regression] snprintf Wformat-truncation
@ 2024-03-18  8:29 torsten.mandel at sap dot com
  2024-03-18  9:25 ` [Bug tree-optimization/114374] [12/13/14 " rguenth at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: torsten.mandel at sap dot com @ 2024-03-18  8:29 UTC (permalink / raw)
  To: gcc-bugs

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.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2024-03-22 13:30 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-18  8:29 [Bug c/114374] New: [12/13 Regression] snprintf Wformat-truncation torsten.mandel at sap dot com
2024-03-18  9:25 ` [Bug tree-optimization/114374] [12/13/14 " rguenth at gcc dot gnu.org
2024-03-18  9:29 ` torsten.mandel at sap dot com
2024-03-18  9:36 ` torsten.mandel at sap dot com
2024-03-18  9:37 ` torsten.mandel at sap dot com
2024-03-18 16:08 ` pinskia at gcc dot gnu.org
2024-03-19 11:31 ` jakub at gcc dot gnu.org
2024-03-19 11:45 ` jakub at gcc dot gnu.org
2024-03-22 13:30 ` law at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).