https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83431
Bug ID: 83431 Summary: -Wformat-truncation may incorrectly report truncation Product: gcc Version: 8.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: bugzi...@poradnik-webmastera.com Target Milestone: --- This looks like another missing optimization - -Wformat-truncation does not take into account that there is "if" which checks that truncation will not happen. [code] #include <stdio.h> #include <string.h> struct S { char str[20]; char out[10]; }; void test(S* s) { if (strlen(s->str) < sizeof(s->out) - 2) snprintf(s->out, sizeof(s->out), "[%s]", s->str); } [/code] [out] $ g++ -c -o test.o test.cc -O2 -Wall test.cc: In function ‘void test(S*)’: test.cc:10:6: warning: ‘%s’ directive output may be truncated writing up to 19 bytes into a region of size 9 [-Wformat-truncation=] void test(S* s) ^~~~ test.cc:13:17: note: ‘snprintf’ output between 3 and 22 bytes into a destination of size 10 snprintf(s->out, sizeof(s->out), "[%s]", s->str); ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [/out] g++ --version g++ (GCC) 8.0.0 20171210 (experimental)