https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79496
Bug ID: 79496
Summary: call to snprintf eliminated with -Wformat-truncation=2
Product: gcc
Version: 7.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: middle-end
Assignee: unassigned at gcc dot gnu.org
Reporter: msebor at gcc dot gnu.org
Target Milestone: ---
As pointed out in bug 79448 comment 3, using -Wformat-truncation=2 results in
eliminating calls to snprintf with a non-constant size argument the lower bound
of whose range is zero. The following small test case demonstrates the
problem.
$ cat u.c && gcc -O2 -S -Wformat-truncation=2
-fdump-tree-printf-return-value=/dev/stdout u.c
void f (void*);
int g (char *d, unsigned n)
{
if (4 < n)
n = 4;
return __builtin_snprintf (d, n, "%i", 123);
}
;; Function g (g, funcdef_no=0, decl_uid=1798, cgraph_uid=0, symbol_order=0)
u.c:7: __builtin_snprintf: objsize = 9223372036854775807, fmtstr = "%i"
Directive 1 at offset 0: "%i"
Result: 3, 3, 3, 3 (3, 3, 3, 3)
Directive 2 at offset 2: "", length = 1
Substituting 3 for statement.
g (char * d, unsigned int n)
{
long unsigned int _1;
int _7;
<bb 2> [100.00%]:
n_8 = MIN_EXPR <n_3(D), 4>;
_1 = (long unsigned int) n_8;
_7 = 3;
return _7;
}