[Bug c/83011] -Wformat-truncation wrongly computes length (depends on the position of numbers in the addition)

julien at trigofacile dot com gcc-bugzilla@gcc.gnu.org
Sat Nov 18 20:52:00 GMT 2017


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

--- Comment #2 from Julien ÉLIE <julien at trigofacile dot com> ---
Created attachment 42644
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=42644&action=edit
Preprocessed translation unit of timer.c

Thanks Martin for your response.
I attached the result of the following command:
/home/iulius/work/gcc/gcc-7.2.0/bin/gcc -g -O3 -DDEBUG=1 -Wall -Werror -Wextra
-Wformat=2 -Wformat-overflow=2 -Wformat-truncation=2 -Wnull-dereference
-Winit-self -Wmissing-include-dirs -Wshift-overflow=2 -Wsync-nand
-Wstringop-overflow=4 -Wmissing-format-attribute -Walloc-zero -Walloca
-Wduplicated-cond -Wtrampolines -Wshadow -Wpointer-arith -Wc99-c11-compat
-Wbad-function-cast -Wcast-align -Wwrite-strings -Wdangling-else -Wdate-time
-Wjump-misses-init -Wstrict-prototypes -Wold-style-definition
-Wmissing-prototypes -Wmissing-declarations -Wnormalized=nfc -Wrestrict
-Wnested-externs -Winline -Winvalid-pch -Wvla -Wno-unused-function 
-I../include -E timer.c -fPIC -DPIC -o .libs/timer.o

In English, the warning is:

timer.c:395:37: error: ' ' directive output may be truncated writing 1 byte
into a region of size between 0 and 1 [-Werror=format-truncation=]
         rc = snprintf(buf, len, "%s ", prefix);
                                    ~ 
timer.c:395:12: note: 'snprintf' output 2 or more bytes (assuming 3) into a
destination of size 1
         rc = snprintf(buf, len, "%s ", prefix);
         ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



I'm not using -flto to build the projet.  And, in case it could help, if I add
"-flto" to compiler options, I no longer have the warning.  Is it normal?


More information about the Gcc-bugs mailing list