https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101451
Bug ID: 101451 Summary: Incorrect -Wstringop-truncation warning Product: gcc Version: 11.1.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: quentin at armitage dot org.uk Target Milestone: --- Created attachment 51151 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51151&action=edit The .i file produced by the compile command Command: gcc -v -save-temps -Wstringop-truncation -O2 -o strncat1 strncat1.c Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/11/lto-wrapper OFFLOAD_TARGET_NAMES=nvptx-none OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-redhat-linux Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-11.1.1-20210531/obj-x86_64-redhat-linux/isl-install --enable-offload-targets=nvptx-none --without-cuda-driver --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 11.1.1 20210531 (Red Hat 11.1.1-3) (GCC) COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wstringop-truncation' '-O2' '-o' 'strncat1' '-mtune=generic' '-march=x86-64' /usr/libexec/gcc/x86_64-redhat-linux/11/cc1 -E -quiet -v strncat1.c -mtune=generic -march=x86-64 -Wstringop-truncation -O2 -fpch-preprocess -o strncat1.i ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/11/include-fixed" ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/11/../../../../x86_64-redhat-linux/include" #include "..." search starts here: #include <...> search starts here: /usr/lib/gcc/x86_64-redhat-linux/11/include /usr/local/include /usr/include End of search list. COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wstringop-truncation' '-O2' '-o' 'strncat1' '-mtune=generic' '-march=x86-64' /usr/libexec/gcc/x86_64-redhat-linux/11/cc1 -fpreprocessed strncat1.i -quiet -dumpbase strncat1.c -dumpbase-ext .c -mtune=generic -march=x86-64 -O2 -Wstringop-truncation -version -o strncat1.s GNU C17 (GCC) version 11.1.1 20210531 (Red Hat 11.1.1-3) (x86_64-redhat-linux) compiled by GNU C version 11.1.1 20210531 (Red Hat 11.1.1-3), GMP version 6.2.0, MPFR version 4.1.0-p13, MPC version 1.2.1, isl version isl-0.18-GMP GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 GNU C17 (GCC) version 11.1.1 20210531 (Red Hat 11.1.1-3) (x86_64-redhat-linux) compiled by GNU C version 11.1.1 20210531 (Red Hat 11.1.1-3), GMP version 6.2.0, MPFR version 4.1.0-p13, MPC version 1.2.1, isl version isl-0.18-GMP GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 Compiler executable checksum: 147699dd2c10be2630d381074a06fb9f strncat1.c: In function ‘bad’: strncat1.c:12:30: warning: ‘strncat’ output may be truncated copying 15 bytes from a string of length 15 [-Wstringop-truncation] 12 | do { dest[0] = '\0'; strncat(dest, src, sizeof(dest) - 1); } while (0); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wstringop-truncation' '-O2' '-o' 'strncat1' '-mtune=generic' '-march=x86-64' as -v --64 -o strncat1.o strncat1.s GNU assembler version 2.35.1 (x86_64-redhat-linux) using BFD version version 2.35.1-41.fc34 COMPILER_PATH=/usr/libexec/gcc/x86_64-redhat-linux/11/:/usr/libexec/gcc/x86_64-redhat-linux/11/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/11/:/usr/lib/gcc/x86_64-redhat-linux/ LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux/11/:/usr/lib/gcc/x86_64-redhat-linux/11/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux/11/../../../:/lib/:/usr/lib/ COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wstringop-truncation' '-O2' '-o' 'strncat1' '-mtune=generic' '-march=x86-64' '-dumpdir' 'strncat1.' /usr/libexec/gcc/x86_64-redhat-linux/11/collect2 -plugin /usr/libexec/gcc/x86_64-redhat-linux/11/liblto_plugin.so -plugin-opt=/usr/libexec/gcc/x86_64-redhat-linux/11/lto-wrapper -plugin-opt=-fresolution=strncat1.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --build-id --no-add-needed --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o strncat1 /usr/lib/gcc/x86_64-redhat-linux/11/../../../../lib64/crt1.o /usr/lib/gcc/x86_64-redhat-linux/11/../../../../lib64/crti.o /usr/lib/gcc/x86_64-redhat-linux/11/crtbegin.o -L/usr/lib/gcc/x86_64-redhat-linux/11 -L/usr/lib/gcc/x86_64-redhat-linux/11/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/11/../../.. strncat1.o -lgcc --push-state --as-needed -lgcc_s --pop-state -lc -lgcc --push-state --as-needed -lgcc_s --pop-state /usr/lib/gcc/x86_64-redhat-linux/11/crtend.o /usr/lib/gcc/x86_64-redhat-linux/11/../../../../lib64/crtn.o COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wstringop-truncation' '-O2' '-o' 'strncat1' '-mtune=generic' '-march=x86-64' '-dumpdir' 'strncat1.' System type =========== Fedora 34 Command line triggering bug =========================== gcc -Wstringop-truncation -O2 -o strncat1 strncat1.c Expected behaviour ================== No -Wstringop-truncation warning Actual behaviour ================ A -Wstringop-truncation warning is issued Other information ================= The warning is not issued unless both -Wstringop-truncation and -O2 are specified. In my code I also get a -Wstringop-truncation warning when using strcpy_safe defined as follows: #define strcpy_safe(dst, src) \ do { strncpy(dst, src, sizeof(dst) - 1); dst[sizeof(dst) - 1] = '\0'; } while (0) However, I cannot produce a simple test case to reproduce it.