At least with a recent GCC 7 trunk build ("gcc (GCC) 7.0.1 20170221 (experimental)"), I noticed that -Wformat-truncation warnings happen to not be emitted if and only if -Og is given: > $ cat test.c > #include <stdio.h> > int main() { > char buf[3]; > snprintf(buf, sizeof buf, "%s", "foo"); > return 0; > } > $ gcc -Wformat-truncation -Og ~/test.c > $ gcc -Wformat-truncation -O ~/test.c > test.c: In function ‘main’: > test.c:4:34: warning: ‘snprintf’ output truncated before the last > format character [-Wformat-truncation=] > snprintf(buf, sizeof buf, "%s", "foo"); > ^ > test.c:4:5: note: ‘snprintf’ output 4 bytes into a destination of size 3 > snprintf(buf, sizeof buf, "%s", "foo"); > ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Any other optimization level (-O0..3/s/fast) does emit the warning. (See mail thread starting at <https://gcc.gnu.org/ml/gcc/2017-02/msg00103.html> "New GCC 7 -Wformat-truncation suppressed by (and only by) -Og?".)
Confirmed for the missing diagnostic (https://gcc.gnu.org/ml/gcc/2017-02/msg00105.html). It's a also a missed optimization because, as can be seen in the test case below, the sprintf return value optimization isn't done: $ (set -x && cat t.c && for O in 1 g; do gcc -O$O -S -Wall -Wextra -Wpedantic -fdump-tree-optimized=/dev/stdout t.c; done) + cat t.c int f (void) { return __builtin_snprintf (0, 0, "%i", 123); } + for O in 1 g + gcc -O1 -S -Wall -Wextra -Wpedantic -fdump-tree-optimized=/dev/stdout t.c ;; Function f (f, funcdef_no=0, decl_uid=1795, cgraph_uid=0, symbol_order=0) f () { <bb 2> [100.00%]: return 3; } + for O in 1 g + gcc -Og -S -Wall -Wextra -Wpedantic -fdump-tree-optimized=/dev/stdout t.c ;; Function f (f, funcdef_no=0, decl_uid=1795, cgraph_uid=0, symbol_order=0) f () { int _3; <bb 2> [100.00%]: _3 = __builtin_snprintf (0B, 0, "%i", 123); return _3; }
Patch posted for review: https://gcc.gnu.org/ml/gcc-patches/2017-02/msg01480.html
Author: msebor Date: Tue Feb 28 16:59:16 2017 New Revision: 245782 URL: https://gcc.gnu.org/viewcvs?rev=245782&root=gcc&view=rev Log: PR tree-optimization/79691 - -Wformat-truncation suppressed by (and only by) -Og gcc/ChangeLog: PR tree-optimization/79691 * passes.def (pass_all_optimizations_g): Enable pass_sprintf_length. gcc/testsuite/ChangeLog: PR tree-optimization/79691 * gcc.dg/tree-ssa/pr79691.c: New test. Added: trunk/gcc/testsuite/gcc.dg/tree-ssa/pr79691.c Modified: trunk/gcc/ChangeLog trunk/gcc/passes.def trunk/gcc/testsuite/ChangeLog
Fixed in r245782.