https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118212
Bug ID: 118212 Summary: False positive -Wstringop-truncation despite [[gnu::nonstring]] Product: gcc Version: 14.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: lukeshu at lukeshu dot com Target Milestone: --- System / GCC version / GCC options: $ gcc -v Using built-in specs. COLLECT_GCC=/usr/bin/gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: /build/gcc/src/gcc/configure --enable-languages=ada,c,c++,d,fortran,go,lto,m2,objc,obj-c++,rust --enable-bootstrap --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://gitlab.archlinux.org/archlinux/packaging/packages/gcc/-/issues --with-build-config=bootstrap-lto --with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit --enable-cet=auto --enable-checking=release --enable-clocale=gnu --enable-default-pie --enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object --enable-libstdcxx-backtrace --enable-link-serialization=1 --enable-linker-build-id --enable-lto --enable-multilib --enable-plugin --enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch --disable-werror Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 14.2.1 20240910 (GCC) Reproducer source code: static struct { [[gnu::nonstring]] char name[16]; } table[1] = {0}; void set_name(unsigned int rownum, const char *name) { __builtin_strncpy(table[rownum].name, name, sizeof(table[0].name)); } Reproducer command and output: $ gcc -O2 -Wstringop-truncation -Werror -c f.c f.c: In function ‘set_name’: f.c:6:3: error: ‘__builtin_strncpy’ specified bound 16 equals destination size [-Werror=stringop-truncation] 6 | __builtin_strncpy(table[rownum].name, name, sizeof(table[0].name)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ cc1: all warnings being treated as errors Notes: This goes away if the size of table is increased beyond `1`, but comes back if rownum is set to the table size minus 1: static struct { [[gnu::nonstring]] char name[16]; } table[3] = {0}; void set_name(const char *name) { unsigned int rownum = 3; __builtin_strncpy(table[rownum-1].name, name, sizeof(table[0].name)); }