Bug 79691 - -Wformat-truncation suppressed by (and only by) -Og points out that the gimple-ssa-sprintf pass doesn't run when this optimization option is used. That's because I forgot to add it to the set of optimization passes that run with that option. The attached trivial patch tested on x86_64 corrects the oversight.
Is this okay for 7.0? Martin
PR tree-optimization/79691 - -Wformat-truncation suppressed by (and only by) -Og gcc/ChangeLog: PR c/79691 * passes.def (pass_all_optimizations_g): Enable pass_sprintf_length. gcc/testsuite/ChangeLog: PR c/79691 * gcc.dg/tree-ssa/pr79691.c: New test. diff --git a/gcc/passes.def b/gcc/passes.def index c09ec22..6b0f05b 100644 --- a/gcc/passes.def +++ b/gcc/passes.def @@ -364,6 +364,7 @@ along with GCC; see the file COPYING3. If not see NEXT_PASS (pass_object_sizes); /* Fold remaining builtins. */ NEXT_PASS (pass_fold_builtins); + NEXT_PASS (pass_sprintf_length, true); /* Copy propagation also copy-propagates constants, this is necessary to forward object-size and builtin folding results properly. */ NEXT_PASS (pass_copy_prop); diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr79691.c b/gcc/testsuite/gcc.dg/tree-ssa/pr79691.c new file mode 100644 index 0000000..badbfcd --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr79691.c @@ -0,0 +1,27 @@ +/* PR tree-optimization/79691 - -Wformat-truncation suppressed by + (and only by) -Og + + { dg-compile } + { dg-options "-Og -Wall" } */ + +char d[2]; + +/* Verify -Wformat-overflow works. */ +void f (void) +{ + __builtin_sprintf (d, "%i", 123); /* { dg-warning "directive writing 3 bytes" } */ +} + +/* Verify -Wformat-truncation works. */ +void g (void) +{ + __builtin_snprintf (d, sizeof d, "%i", 1234); /* { dg-warning "output truncated writing 4 bytes" } */ +} + +/* Verify -fprintf-return-value works. */ +int h (void) +{ + return __builtin_snprintf (0, 0, "%i", 12345); +} + +/* { dg-final { scan-tree-dump-not "snprintf" "optimized" } } */