Bug 79691 - -Wformat-truncation suppressed by (and only by) -Og
Summary: -Wformat-truncation suppressed by (and only by) -Og
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 7.0.1
: P3 normal
Target Milestone: ---
Assignee: Martin Sebor
URL:
Keywords: diagnostic, missed-optimization, patch
Depends on:
Blocks:
 
Reported: 2017-02-23 16:14 UTC by Stephan Bergmann
Modified: 2017-02-28 17:01 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2017-02-23 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Stephan Bergmann 2017-02-23 16:14:16 UTC
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?".)
Comment 1 Martin Sebor 2017-02-23 16:33:18 UTC
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;

}
Comment 2 Martin Sebor 2017-02-24 00:35:56 UTC
Patch posted for review:
https://gcc.gnu.org/ml/gcc-patches/2017-02/msg01480.html
Comment 3 Martin Sebor 2017-02-28 16:59:53 UTC
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
Comment 4 Martin Sebor 2017-02-28 17:01:23 UTC
Fixed in r245782.