[Bug c/80354] New: Poor support to silence -Wformat-truncation=1
sbergman at redhat dot com
gcc-bugzilla@gcc.gnu.org
Fri Apr 7 10:23:00 GMT 2017
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80354
Bug ID: 80354
Summary: Poor support to silence -Wformat-truncation=1
Product: gcc
Version: 7.0.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: sbergman at redhat dot com
Target Milestone: ---
With recent trunk
> $ gcc --version
> gcc (GCC) 7.0.1 20170406 (experimental)
> Copyright (C) 2017 Free Software Foundation, Inc.
> This is free software; see the source for copying conditions. There is NO
> warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
and test.c
> #include <stdio.h>
> struct S { char a[3]; };
> void f1(struct S * s, int n) {
> char b[2];
> (void) snprintf(b, sizeof (b), "%s%d", s->a, n);
> }
> void f2(struct S * s, int n) {
> char b[2];
> int e = snprintf(b, sizeof (b), "%s%d", s->a, n);
> (void) e;
> }
> void f3(struct S * s, int n) {
> char b[2];
> int e = snprintf(b, sizeof (b), "%s%d", s->a, n);
> if (e >= sizeof (b)) {}
> }
> void f4(struct S * s, int n) {
> char b[2];
> int e = snprintf(b, sizeof (b), "%s%d", s->a, n);
> if (e >= sizeof (b)) b[1] = 0;
> }
the documentation of -Wformat-truncation=1 (as enabled by -Wall) in
gcc/doc/invoke.texi states it "warns only about calls to bounded functions
whose return value is unused [...]"
However, depending on e.g. -O0 vs. -Og, GCC will warn about f1 (gcc -Wall -O0
-c test.c) or even f1, f2, f3 (gcc -Wall -Og -c test.c), while I think it
should warn about none:
> test.c: In function ‘f1’:
> test.c:5:39: warning: ‘%d’ directive output may be truncated writing between 1 and 11 bytes into a region of size between 0 and 2 [-Wformat-truncation=]
> (void) snprintf(b, sizeof (b), "%s%d", s->a, n);
> ^~
> test.c:5:5: note: ‘snprintf’ output between 2 and 14 bytes into a destination of size 2
> (void) snprintf(b, sizeof (b), "%s%d", s->a, n);
> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> test.c: In function ‘f2’:
> test.c:9:40: warning: ‘%d’ directive output may be truncated writing between 1 and 11 bytes into a region of size between 0 and 2 [-Wformat-truncation=]
> int e = snprintf(b, sizeof (b), "%s%d", s->a, n);
> ^~
> test.c:9:9: note: ‘snprintf’ output between 2 and 14 bytes into a destination of size 2
> int e = snprintf(b, sizeof (b), "%s%d", s->a, n);
> ^
> test.c: In function ‘f3’:
> test.c:14:40: warning: ‘%d’ directive output may be truncated writing between 1 and 11 bytes into a region of size between 0 and 2 [-Wformat-truncation=]
> int e = snprintf(b, sizeof (b), "%s%d", s->a, n);
> ^~
> test.c:14:9: note: ‘snprintf’ output between 2 and 14 bytes into a destination of size 2
> int e = snprintf(b, sizeof (b), "%s%d", s->a, n);
> ^
More information about the Gcc-bugs
mailing list