https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88835

--- Comment #14 from Mark Wielaard <mark at gcc dot gnu.org> ---
(In reply to Mark Wielaard from comment #12)
> (In reply to Martin Sebor from comment #11)
> > Ah, but you mentioned elfutilts, not binutils.  I've now downloaded and
> > built elfutils-0.175.  It took a bit more effort because --disable-werror
> > doesn't work there but once I got past that I just got the three
> > -Wformat-truncation instances below:
> > 
> > Diagnostic                        Count   Unique    Files
> > -Wformat-truncation=                  3        3        2
> > 
> > -Wformat-truncation Instances:
> >   /src/elfutils-0.175/src/ar.c:1468
> >   /src/elfutils-0.175/src/ar.c:859
> >   /src/elfutils-0.175/src/arlib.c:63
> 
> I am not seeing these, but they might have been fixed in git. We like to
> keep the code warning free since we always build with -Werror.

Aha, I now see, you are using -Wformat-truncation=2. Then yes, these snprintfs
formats could produce more characters than would fit in the given buffer/size.
But that is kind of the point of the code, that we don't overflow the given
buffer. The snprintf is supposed to truncate to what would fit in these cases.
I can see if I could come up with something smarter to detect this without
using snprintf, but that seems to defeat the point of using snprintf. So for
now we just don't use -Wformat-truncation=2. (Background, ar files are weird,
they use fixed size character fields for numbers as decimal strings without a
zero terminator, but right padded with spaces.)

The specific warnings which we enable can be found in config/eu.am and depend
on some configure checks to make sure gcc supports them:

AM_CFLAGS = -std=gnu99 -Wall -Wshadow -Wformat=2 \
            -Wold-style-definition -Wstrict-prototypes -Wtrampolines \
            $(LOGICAL_OP_WARNING) $(DUPLICATED_COND_WARNING) \
            $(NULL_DEREFERENCE_WARNING) $(IMPLICIT_FALLTHROUGH_WARNING) \
            $(if $($(*F)_no_Werror),,-Werror) \
            $(if $($(*F)_no_Wunused),,-Wunused -Wextra) \
            $(if $($(*F)_no_Wstack_usage),,$(STACK_USAGE_WARNING)) \
            $(if $($(*F)_no_Wpacked_not_aligned),-Wno-packed-not-aligned,) \
            $($(*F)_CFLAGS)

With the following (if supported):

STACK_USAGE_WARNING=-Wstack-usage=262144
LOGICAL_OP_WARNING=-Wlogical-op
DUPLICATED_COND_WARNING=-Wduplicated-cond
NULL_DEREFERENCE_WARNING=-Wnull-dereference
IMPLICIT_FALLTHROUGH_WARNING=-Wimplicit-fallthrough=5

As you can see individual files can turn off some of these if necessary in the
Makefile by adding file_no_Wxxx. So the easiest way to see which warnings are
used it by running make V=1 which for this specific case gives (note the -m32
since I am running this on x86_64):

gcc -D_GNU_SOURCE -DHAVE_CONFIG_H -DLOCALEDIR='"/usr/local/share/locale"' 
-DDEBUGPRED=0 -DSRCDIR=\"/home/mark/src/elfutils/src\"
-DOBJDIR=\"/opt/local/build/elfutils-obj/src\" -I.
-I/home/mark/src/elfutils/src -I..  -I. -I/home/mark/src/elfutils/src
-I/home/mark/src/elfutils/lib -I.. -I/home/mark/src/elfutils/src/../libelf
-I/home/mark/src/elfutils/src/../libebl -I/home/mark/src/elfutils/src/../libdw
-I/home/mark/src/elfutils/src/../libdwelf
-I/home/mark/src/elfutils/src/../libdwfl
-I/home/mark/src/elfutils/src/../libasm  -std=gnu99 -Wall -Wshadow -Wformat=2
-Wold-style-definition -Wstrict-prototypes -Wtrampolines -Wlogical-op
-Wduplicated-cond -Wnull-dereference -Wimplicit-fallthrough=5 -Werror -Wunused
-Wextra    -D_FORTIFY_SOURCE=2 -m32 -g -O2 -DBAD_FTS=1 -MT readelf.o -MD -MP
-MF .deps/readelf.Tpo -c -o readelf.o /home/mark/src/elfutils/src/readelf.c
/home/mark/src/elfutils/src/readelf.c: In function ‘print_debug_str_section’:
/home/mark/src/elfutils/src/readelf.c:10167:15: error: ‘%*llx’ directive output
between 4 and 2147483647 bytes may cause result to exceed ‘INT_MAX’
[-Werror=format-overflow=]
10167 |       printf (" [%*" PRIx64 "]  \"%s\"\n", digits, (uint64_t) offset,
str);
      |               ^~~~~~
/home/mark/src/elfutils/src/readelf.c:10167:18: note: format string is defined
here
10167 |       printf (" [%*" PRIx64 "]  \"%s\"\n", digits, (uint64_t) offset,
str);
/home/mark/src/elfutils/src/readelf.c:10167:15: note: directive argument in the
range [0, 18446744073709551614]
10167 |       printf (" [%*" PRIx64 "]  \"%s\"\n", digits, (uint64_t) offset,
str);
      |               ^~~~~~
cc1: all warnings being treated as errors

Reply via email to