https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79257
Bug ID: 79257 Summary: spurious -Wformat-overflow=1 warning with -O2 and sanitizer Product: gcc Version: 7.0.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: vincent-gcc at vinc17 dot net Target Milestone: --- With r244974, on the following C code: #include <stdio.h> void a (void); int main (void) { char buffer[2]; int i; for (i = 0; i < 2; i++) { if (i == 0) a (); sprintf (buffer, "%d", i); } return 0; } I get: cventin:~> =gcc -O2 -Wformat-overflow=1 -fsanitize=undefined -c tst.c tst.c: In function ‘main’: tst.c:14:25: warning: ‘%d’ directive writing between 1 and 10 bytes into a region of size 2 [-Wformat-overflow=] sprintf (buffer, "%d", i); ^~ tst.c:14:24: note: directive argument in the range [0, 2147483646] sprintf (buffer, "%d", i); ^~~~ tst.c:14:7: note: ‘sprintf’ output between 2 and 11 bytes into a destination of size 2 sprintf (buffer, "%d", i); ^~~~~~~~~~~~~~~~~~~~~~~~~ tst.c:14:25: warning: ‘%d’ directive writing between 1 and 10 bytes into a region of size 2 [-Wformat-overflow=] sprintf (buffer, "%d", i); ^~ tst.c:14:24: note: directive argument in the range [0, 2147483646] sprintf (buffer, "%d", i); ^~~~ tst.c:14:7: note: ‘sprintf’ output between 2 and 11 bytes into a destination of size 2 sprintf (buffer, "%d", i); ^~~~~~~~~~~~~~~~~~~~~~~~~ If I remove any of -O2, -Wformat-overflow=1 or -fsanitize=undefined, I no longer get a warning. Ditto if I remove the test on i or the call to a(). Note: I've found this bug when building the MPFR tests (tl2b.c).