https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78606
Bug ID: 78606 Summary: -Wformat-length/-fprintf-return-value incorrect for %+.0i and %.0o with zero value Product: gcc Version: 7.0 Status: UNCONFIRMED Severity: minor Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- There's an obscure rule in C that says that an integer conversion specification with a zero precision and zero value (such as a "%.0i") results in bytes. There's an even more obscure rule that implies that the + flag (but no other flag) results in a sole plus sign being output when used in such a directive (i.e., "%+.0i"). And yet another obscure rule says that when used with the o conversion (but none other), the # flag has the effect of increasing zero precision from zero to 1 when used with a zero argument so that "%#.0o" results in a 0 on output (while "%#.0x" results in nothing). The test case below shows that the gimple-ssa-sprintf pass doesn't get these subtleties right. $ (set -x; cat c.c && for f in '"%+.0i"' '"%#.0o"'; do /build/gcc-git/gcc/xgcc -B /build/gcc-git/gcc -DFMT="$f" -O2 -Wextra -Wformat-length c.c && ./a.out; done) + cat c.c const char* fmt = FMT; int main (void) { int n = __builtin_snprintf (0, 0, FMT, 0); if (n != __builtin_snprintf (0, 0, fmt, 0)) __builtin_abort (); } + for f in ''\''"%+.0i"'\''' ''\''"%#.0o"'\''' + /build/gcc-git/gcc/xgcc -B /build/gcc-git/gcc '-DFMT="%+.0i"' -O2 -Wextra -Wformat-length c.c + ./a.out + for f in ''\''"%+.0i"'\''' ''\''"%#.0o"'\''' + /build/gcc-git/gcc/xgcc -B /build/gcc-git/gcc '-DFMT="%#.0o"' -O2 -Wextra -Wformat-length c.c + ./a.out