https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97628
Bug ID: 97628 Summary: format truncation false positive for O1 and mtune=nocona Product: gcc Version: 9.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: walter.gcc at wjd dot nu Target Milestone: --- Created attachment 49468 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=49468&action=edit trimmed down problem from Asterisk say.c Per the discussion here: https://gerrit.asterisk.org/c/asterisk/+/14501 I believe there is a bug in gcc 9 where it falsely reports: warning: ‘%d’ directive output may be truncated writing between 1 and 11 bytes into a region of size 10 [-Wformat-truncation=] See the attached example, it only triggers when: - optimization is 1 or higher - arch tuning is set to nocona Example: $ gcc -O1 -mtune=nocona -S -Wall say2.c say2.c: In function ‘ast_say_number_full_zh’: say2.c:37:32: warning: ‘%d’ directive output may be truncated writing between 1 and 11 bytes into a region of size 10 [-Wformat-truncation=] 37 | snprintf(buf, 10, "%d", num); | ^~ say2.c:37:31: note: directive argument in the range [-2147483648, 9] ... But when omitting either -O1 or -mtune=nocona we do not get this warning. And if you read the code, you would see that at line 37, num cannot be negative (because it would've hit a different if-branch). gcc version is the Ubuntu/Focal one: $ gcc --version gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0 Cheers, Walter Doekes OSSO B.V.