https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103173
Bug ID: 103173 Summary: strncpy output may be truncated copying 32 bytes from a string of length 1439 (bogus) [-Werror=stringop-truncation] Product: gcc Version: 11.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: patrickdepinguin at gmail dot com Target Milestone: --- gcc 11.2.0 gives a bogus warning of type 'stringop-truncation' on below reduced test case. .../arm-cortex_a53-linux-gnueabi-gcc -O2 -Wall -Werror -c /tmp/test.c -o /tmp/foo.o /tmp/test.c: In function 'func': /tmp/test.c:22:5: error: 'strncpy' output may be truncated copying 32 bytes from a string of length 1439 [-Werror=stringop-truncation] 22 | strncpy(dest, data[j].name, sizeof(dest)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ cc1: all warnings being treated as errors ----------------- #include <string.h> struct data_struct { char name[32]; int bar; }; void func() { extern struct data_struct data[40]; extern char dest[32]; int j=0; strncpy(dest, data[j].name, sizeof(dest)); }; ----------------- Here 'dest' is correctly interpreted as an array of size 32, but 'data[j].name' is interpreted as having size 1439 which is incorrect, as it should also have size 32 (name member of struct data_struct). Following factors influence the reported size 1439: * if data_struct is given more or less additional members, like 'bar' * if the array length of 'data' (40) is changed * if the length of the 'name' array in data_struct is changed If 'j' is replaced by 0 directly, as in: strncpy(dest, data[0].name, sizeof(dest)); the error disappears. This problem did not occur on our previous toolchain with gcc 7.4.0 for the same architecture, nor for x86_64 with gcc 7.5.0. Info from gcc -v: Using built-in specs. COLLECT_GCC=.../buildroot-toolchains/output/host/opt/ext-toolchain/bin/arm-cortex_a53-linux-gnueabi-gcc COLLECT_LTO_WRAPPER=.../buildroot-toolchains/output/host/opt/ext-toolchain/arm/bin/../libexec/gcc/arm-cortex_a53-linux-gnueabi/11.2.0/lto-wrapper Target: arm-cortex_a53-linux-gnueabi Configured with: .../crosstool-ng/.build/arm-cortex_a53-linux-gnueabi/src/gcc/configure --build=x86_64-build_pc-linux-gnu --host=x86_64-build_pc-linux-gnu --target=arm-cortex_a53-linux-gnueabi --prefix=.../crosstool-ng/targets/arm-cortex_a53-linux-gnueabi --exec_prefix=.../crosstool-ng/targets/arm-cortex_a53-linux-gnueabi --with-sysroot=.../crosstool-ng/targets/arm-cortex_a53-linux-gnueabi/arm-cortex_a53-linux-gnueabi/sysroot --enable-languages=c,c++,fortran --with-cpu=cortex-a53 --with-fpu=neon-fp-armv8 --with-float=hard --with-pkgversion='crosstool-NG 1.24.0.487_10ac846' --enable-__cxa_atexit --disable-libmudflap --disable-libgomp --disable-libssp --disable-libquadmath --disable-libquadmath-support --disable-libsanitizer --disable-libmpx --with-gmp=.../crosstool-ng/.build/arm-cortex_a53-linux-gnueabi/buildtools --with-mpfr=.../crosstool-ng/.build/arm-cortex_a53-linux-gnueabi/buildtools --with-mpc=.../crosstool-ng/.build/arm-cortex_a53-linux-gnueabi/buildtools --with-isl=.../crosstool-ng/.build/arm-cortex_a53-linux-gnueabi/buildtools --disable-lto --without-zstd --enable-threads=posix --enable-target-optspace --disable-plugin --disable-nls --disable-multilib --with-local-prefix=.../crosstool-ng/targets/arm-cortex_a53-linux-gnueabi/arm-cortex_a53-linux-gnueabi/sysroot --enable-long-long Thread model: posix Supported LTO compression algorithms: zlib gcc version 11.2.0 (crosstool-NG 1.24.0.487_10ac846)