https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100780
Bug ID: 100780 Summary: __builtin___sprintf_chk not optimized when it could be Product: gcc Version: 11.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- Calls to __builtin___sprintf_chk that can be proven not to overflow the destination can be folded to calls to plain sprintf (or even memcpy). Only the simplest are, but the more interesting ones aren't, even though the sprintf pass has all the smarts to do it. $ cat a.c && gcc -O2 -S -Wall -fdump-tree-optimized=/dev/stdout a.c extern char a[32]; void f (void) { __builtin___sprintf_chk (a, 0, sizeof a, "%s", "1234"); // optimized } void g (void) { __builtin___sprintf_chk (a, 0, sizeof a, "%i", 1234); // not optimized } ;; Function f (f, funcdef_no=0, decl_uid=1944, cgraph_uid=1, symbol_order=0) void f () { <bb 2> [local count: 1073741824]: __builtin_memcpy (&a, "1234", 5); [tail call] return; } ;; Function g (g, funcdef_no=1, decl_uid=1947, cgraph_uid=2, symbol_order=1) void g () { <bb 2> [local count: 1073741824]: __builtin___sprintf_chk (&a, 0, 32, "%i", 1234); [tail call] return; }