Bug 77675 - optimization level==number of -Wformat-length warnings
Summary: optimization level==number of -Wformat-length warnings
Status: RESOLVED DUPLICATE of bug 77708
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 7.0
: P3 normal
Target Milestone: ---
Assignee: Martin Sebor
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-09-21 11:23 UTC by Markus Trippelsdorf
Modified: 2017-01-11 17:57 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2016-09-22 00:00:00


Attachments
unreduced testcase (219.03 KB, application/x-xz)
2016-09-21 11:23 UTC, Markus Trippelsdorf
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Markus Trippelsdorf 2016-09-21 11:23:51 UTC
Created attachment 39666 [details]
unreduced testcase

Consider the attached unreduced testcase. The number of redundant warnings increases with the -O level due to inlining:

markus@x4 /tmp % gcc -O1 -c -Wformat-length algapi.i
crypto/algapi.c: In function ‘crypto_inst_setname’:
crypto/algapi.c:817:6: warning: output may be truncated at or before format character ‘)’ at offset 5 [-Wformat-length=]
crypto/algapi.c:817:6: note: format output between 3 and 66 bytes into a destination of size 64

markus@x4 /tmp % cc -O2 -c -Wformat-length algapi.i
crypto/algapi.c: In function ‘crypto_inst_setname’:
crypto/algapi.c:817:6: warning: output may be truncated at or before format character ‘)’ at offset 5 [-Wformat-length=]
crypto/algapi.c:817:6: note: format output between 3 and 66 bytes into a destination of size 64
crypto/algapi.c: In function ‘crypto_alloc_instance2’:
crypto/algapi.c:817:6: warning: output may be truncated at or before format character ‘)’ at offset 5 [-Wformat-length=]
crypto/algapi.c:817:6: note: format output between 3 and 66 bytes into a destination of size 64

markus@x4 /tmp % gcc -O3 -c -Wformat-length algapi.i
crypto/algapi.c: In function ‘crypto_inst_setname’:
crypto/algapi.c:817:6: warning: output may be truncated at or before format character ‘)’ at offset 5 [-Wformat-length=]
crypto/algapi.c:817:6: note: format output between 3 and 66 bytes into a destination of size 64
crypto/algapi.c: In function ‘crypto_alloc_instance2’:
crypto/algapi.c:817:6: warning: output may be truncated at or before format character ‘)’ at offset 5 [-Wformat-length=]
crypto/algapi.c:817:6: note: format output between 3 and 66 bytes into a destination of size 64
crypto/algapi.c: In function ‘crypto_alloc_instance’:
crypto/algapi.c:817:6: warning: output may be truncated at or before format character ‘)’ at offset 5 [-Wformat-length=]
crypto/algapi.c:817:6: note: format output between 3 and 66 bytes into a destination of size 64
Comment 1 Martin Sebor 2016-09-22 03:15:04 UTC
The output with the latest patch (https://gcc.gnu.org/ml/gcc-patches/2016-09/msg01477.html) I see the warnings below, but only with -Wformat-length=2.

If I understand correctly, you're wondering if (or implying that) there should be just one warning in each compiler invocation, regardless of the optimization level.  It seems reasonable in this case but I confess I don't know if it's possible.  The pass relies on optimization for constant propagation and value range propagation, and those in turn depend on inlining.  With more function calls inlined it's possible for the same sprintf call to be duplicated in more callers and result in more warnings than otherwise.  Maybe there's a way to set TREE_NO_WARNING for the call statement after it has been diagnosed.  Let me look into it.

 (set -x; for o in 0 1 2 3; do /build/gcc-77676/gcc/xgcc -B /build/gcc-77676/gcc -O$o -S -Wformat-length=2  ~/tmp/algapi.i; done)
+ for o in 0 1 2 3
+ /build/gcc-77676/gcc/xgcc -B /build/gcc-77676/gcc -O0 -S -Wformat-length=2 /home/msebor/tmp/algapi.i
crypto/algapi.c: In function ‘crypto_inst_setname’:
crypto/algapi.c:817:6: warning: ‘%s’ directive output may be truncated writing between 1 and 63 bytes into a region of size 62 [-Wformat-length=]
crypto/algapi.c:817:6: note: format output between 5 and 67 bytes into a destination of size 64
+ for o in 0 1 2 3
+ /build/gcc-77676/gcc/xgcc -B /build/gcc-77676/gcc -O1 -S -Wformat-length=2 /home/msebor/tmp/algapi.i
crypto/algapi.c: In function ‘crypto_inst_setname’:
crypto/algapi.c:817:6: warning: ‘%s’ directive output may be truncated writing between 1 and 63 bytes into a region of size 62 [-Wformat-length=]
crypto/algapi.c:817:6: note: format output between 5 and 67 bytes into a destination of size 64
+ for o in 0 1 2 3
+ /build/gcc-77676/gcc/xgcc -B /build/gcc-77676/gcc -O2 -S -Wformat-length=2 /home/msebor/tmp/algapi.i
crypto/algapi.c: In function ‘crypto_inst_setname’:
crypto/algapi.c:817:6: warning: ‘%s’ directive output may be truncated writing between 1 and 63 bytes into a region of size 62 [-Wformat-length=]
crypto/algapi.c:817:6: note: format output between 5 and 67 bytes into a destination of size 64
crypto/algapi.c: In function ‘crypto_alloc_instance2’:
crypto/algapi.c:817:6: warning: ‘%s’ directive output may be truncated writing between 1 and 63 bytes into a region of size 62 [-Wformat-length=]
crypto/algapi.c:817:6: note: format output between 5 and 67 bytes into a destination of size 64
+ for o in 0 1 2 3
+ /build/gcc-77676/gcc/xgcc -B /build/gcc-77676/gcc -O3 -S -Wformat-length=2 /home/msebor/tmp/algapi.i
crypto/algapi.c: In function ‘crypto_inst_setname’:
crypto/algapi.c:817:6: warning: ‘%s’ directive output may be truncated writing between 1 and 63 bytes into a region of size 62 [-Wformat-length=]
crypto/algapi.c:817:6: note: format output between 5 and 67 bytes into a destination of size 64
crypto/algapi.c: In function ‘crypto_alloc_instance2’:
crypto/algapi.c:817:6: warning: ‘%s’ directive output may be truncated writing between 1 and 63 bytes into a region of size 62 [-Wformat-length=]
crypto/algapi.c:817:6: note: format output between 5 and 67 bytes into a destination of size 64
crypto/algapi.c: In function ‘crypto_alloc_instance’:
crypto/algapi.c:817:6: warning: ‘%s’ directive output may be truncated writing between 1 and 63 bytes into a region of size 62 [-Wformat-length=]
crypto/algapi.c:817:6: note: format output between 5 and 67 bytes into a destination of size 64
Comment 2 Martin Sebor 2017-01-11 17:57:50 UTC
This seems to have been fixed as a result of introducing -Wformat-truncation in r244210.  Resolving as a duplicate of 77708.

For the record, here's the output at the default setting of the -Wformat-length and -Wformat-truncation options with today's top of trunk:

$ (set -x; for o in 0 1 2; do gcc -O$o -S -Wformat -Wformat-length -Wformat-truncation algapi.i; done)
+ for o in 0 1 2
+ /build/gcc-svn/gcc/xgcc -B /build/gcc-svn/gcc -O0 -S -Wformat -Wformat-length -Wformat-truncation algapi.i
+ for o in 0 1 2
+ /build/gcc-svn/gcc/xgcc -B /build/gcc-svn/gcc -O1 -S -Wformat -Wformat-length -Wformat-truncation algapi.i
+ for o in 0 1 2
+ /build/gcc-svn/gcc/xgcc -B /build/gcc-svn/gcc -O2 -S -Wformat -Wformat-length -Wformat-truncation algapi.i

And here's the output at level 2:

$ (set -x; for o in 0 1 2 3; do gcc -O$o -S -Wformat -Wformat-length=2 -Wformat-truncation=2 algapi.i; done)
+ for o in 0 1 2 3
+ /build/gcc-svn/gcc/xgcc -B /build/gcc-svn/gcc -O0 -S -Wformat -Wformat-length=2 -Wformat-truncation=2 algapi.i
crypto/algapi.c: In function ‘crypto_inst_setname’:
crypto/algapi.c:817:6: warning: ‘%s’ directive output may be truncated writing between 1 and 63 bytes into a region of size 62 [-Wformat-truncation=]
crypto/algapi.c:817:6: note: format output between 5 and 67 bytes into a destination of size 64
+ for o in 0 1 2 3
+ /build/gcc-svn/gcc/xgcc -B /build/gcc-svn/gcc -O1 -S -Wformat -Wformat-length=2 -Wformat-truncation=2 algapi.i
crypto/algapi.c: In function ‘crypto_inst_setname’:
crypto/algapi.c:817:6: warning: ‘%s’ directive output may be truncated writing between 1 and 63 bytes into a region of size 62 [-Wformat-truncation=]
crypto/algapi.c:817:6: note: format output between 5 and 67 bytes into a destination of size 64
+ for o in 0 1 2 3
+ /build/gcc-svn/gcc/xgcc -B /build/gcc-svn/gcc -O2 -S -Wformat -Wformat-length=2 -Wformat-truncation=2 algapi.i
crypto/algapi.c: In function ‘crypto_inst_setname’:
crypto/algapi.c:817:6: warning: ‘%s’ directive output may be truncated writing between 1 and 63 bytes into a region of size 62 [-Wformat-truncation=]
crypto/algapi.c:817:6: note: format output between 5 and 67 bytes into a destination of size 64
crypto/algapi.c: In function ‘crypto_alloc_instance2’:
crypto/algapi.c:817:6: warning: ‘%s’ directive output may be truncated writing between 1 and 63 bytes into a region of size 62 [-Wformat-truncation=]
crypto/algapi.c:817:6: note: format output between 5 and 67 bytes into a destination of size 64
+ for o in 0 1 2 3
+ /build/gcc-svn/gcc/xgcc -B /build/gcc-svn/gcc -O3 -S -Wformat -Wformat-length=2 -Wformat-truncation=2 algapi.i
crypto/algapi.c: In function ‘crypto_inst_setname’:
crypto/algapi.c:817:6: warning: ‘%s’ directive output may be truncated writing between 1 and 63 bytes into a region of size 62 [-Wformat-truncation=]
crypto/algapi.c:817:6: note: format output between 5 and 67 bytes into a destination of size 64
crypto/algapi.c: In function ‘crypto_alloc_instance2’:
crypto/algapi.c:817:6: warning: ‘%s’ directive output may be truncated writing between 1 and 63 bytes into a region of size 62 [-Wformat-truncation=]
crypto/algapi.c:817:6: note: format output between 5 and 67 bytes into a destination of size 64
crypto/algapi.c: In function ‘crypto_alloc_instance’:
crypto/algapi.c:817:6: warning: ‘%s’ directive output may be truncated writing between 1 and 63 bytes into a region of size 62 [-Wformat-truncation=]
crypto/algapi.c:817:6: note: format output between 5 and 67 bytes into a destination of size 64

*** This bug has been marked as a duplicate of bug 77708 ***