From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 48BAC3985447; Wed, 14 Jul 2021 13:26:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 48BAC3985447 From: "quentin at armitage dot org.uk" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/101451] New: Incorrect -Wstringop-truncation warning Date: Wed, 14 Jul 2021 13:26:33 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 11.1.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: quentin at armitage dot org.uk X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone attachments.created Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 14 Jul 2021 13:26:34 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D101451 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=3D51151&action=3Dedit 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=3Dgcc COLLECT_LTO_WRAPPER=3D/usr/libexec/gcc/x86_64-redhat-linux/11/lto-wrapper OFFLOAD_TARGET_NAMES=3Dnvptx-none OFFLOAD_TARGET_DEFAULT=3D1 Target: x86_64-redhat-linux Configured with: ../configure --enable-bootstrap --enable-languages=3Dc,c++,fortran,objc,obj-c++,ada,go,d,lto --prefix=3D/usr --mandir=3D/usr/share/man --infodir=3D/usr/share/info --with-bugurl=3Dhttp://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=3Dposix --enable-checking=3Drelease --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=3Dgnu --enable-plugin --enable-initfini-array --with-isl=3D/builddir/build/BUILD/gcc-11.1.1-20210531/obj-x86_64-redhat-li= nux/isl-install --enable-offload-targets=3Dnvptx-none --without-cuda-driver --enable-gnu-indirect-function --enable-cet --with-tune=3Dgeneric --with-arch_32=3Di686 --build=3Dx86_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)=20 COLLECT_GCC_OPTIONS=3D'-v' '-save-temps' '-Wstringop-truncation' '-O2' '-o' 'strncat1' '-mtune=3Dgeneric' '-march=3Dx86-64' /usr/libexec/gcc/x86_64-redhat-linux/11/cc1 -E -quiet -v strncat1.c -mtune=3Dgeneric -march=3Dx86-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/includ= e" #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=3D'-v' '-save-temps' '-Wstringop-truncation' '-O2' '-o' 'strncat1' '-mtune=3Dgeneric' '-march=3Dx86-64' /usr/libexec/gcc/x86_64-redhat-linux/11/cc1 -fpreprocessed strncat1.i -qui= et -dumpbase strncat1.c -dumpbase-ext .c -mtune=3Dgeneric -march=3Dx86-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-lin= ux) 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=3D100 --param ggc-min-heapsize=3D131= 072 GNU C17 (GCC) version 11.1.1 20210531 (Red Hat 11.1.1-3) (x86_64-redhat-lin= ux) 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=3D100 --param ggc-min-heapsize=3D131= 072 Compiler executable checksum: 147699dd2c10be2630d381074a06fb9f strncat1.c: In function =E2=80=98bad=E2=80=99: strncat1.c:12:30: warning: =E2=80=98strncat=E2=80=99 output may be truncate= d copying 15 bytes from a string of length 15 [-Wstringop-truncation] 12 | do { dest[0] =3D '\0'; strncat(dest, src, sizeof(dest) - 1)= ; } while (0); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ COLLECT_GCC_OPTIONS=3D'-v' '-save-temps' '-Wstringop-truncation' '-O2' '-o' 'strncat1' '-mtune=3Dgeneric' '-march=3Dx86-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=3D/usr/libexec/gcc/x86_64-redhat-linux/11/:/usr/libexec/gcc/x= 86_64-redhat-linux/11/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x= 86_64-redhat-linux/11/:/usr/lib/gcc/x86_64-redhat-linux/ LIBRARY_PATH=3D/usr/lib/gcc/x86_64-redhat-linux/11/:/usr/lib/gcc/x86_64-red= hat-linux/11/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/= gcc/x86_64-redhat-linux/11/../../../:/lib/:/usr/lib/ COLLECT_GCC_OPTIONS=3D'-v' '-save-temps' '-Wstringop-truncation' '-O2' '-o' 'strncat1' '-mtune=3Dgeneric' '-march=3Dx86-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=3D/usr/libexec/gcc/x86_64-redhat-linux/11/lto-wrapper -plugin-opt=3D-fresolution=3Dstrncat1.res -plugin-opt=3D-pass-through=3D-lg= cc -plugin-opt=3D-pass-through=3D-lgcc_s -plugin-opt=3D-pass-through=3D-lc -plugin-opt=3D-pass-through=3D-lgcc -plugin-opt=3D-pass-through=3D-lgcc_s -= -build-id --no-add-needed --eh-frame-hdr --hash-style=3Dgnu -m elf_x86_64 -dynamic-li= nker /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=3D'-v' '-save-temps' '-Wstringop-truncation' '-O2' '-o' 'strncat1' '-mtune=3Dgeneric' '-march=3Dx86-64' '-dumpdir' 'strncat1.' System type =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Fedora 34 Command line triggering bug =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D gcc -Wstringop-truncation -O2 -o strncat1 strncat1.c Expected behaviour =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D No -Wstringop-truncation warning Actual behaviour =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D A -Wstringop-truncation warning is issued Other information =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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] =3D '= \0'; } while (0) However, I cannot produce a simple test case to reproduce it.=