https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102875
Bug ID: 102875 Summary: __builtin_strncpy output may be truncated copying bytes from a string of length Product: gcc Version: 9.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: johnnymarler at gmail dot com Target Milestone: --- GCC Version: 9.2.0 System: NixOS $ gcc -v Using built-in specs. COLLECT_GCC=/nix/store/b3zsk4ihlpiimv3vff86bb5bxghgdzb9-gcc-9.2.0/bin/gcc COLLECT_LTO_WRAPPER=/nix/store/b3zsk4ihlpiimv3vff86bb5bxghgdzb9-gcc-9.2.0/libexec/gcc/x86_64-unknown-linux-gnu/9.2.0/lto-wrapper Target: x86_64-unknown-linux-gnu Configured with: Thread model: posix gcc version 9.2.0 (GCC) How to reproduce: wtf.c ------------------------------ #include <string.h> // value of 2 works??? //#define COPY_COUNT 2 // values >3 don't work #define COPY_COUNT 3 typedef struct { // NOTE: these padding values don't fix it //int something_to_pad_between_elements_1; char array[100]; //int something_to_pad_between_elements_2; } StringBuf; int main() { StringBuf a[COPY_COUNT]; StringBuf b[COPY_COUNT]; for (unsigned i = 0; i < COPY_COUNT; i++) { strncpy(a[i].array, b[i].array, 100); } } ------------------------------ $ gcc -Wall wtf.c In file included from /nix/store/fwpn2f7a4iqszyydw7ag61zlnp6xk5d3-glibc-2.30-dev/include/string.h:494, from wtf.c:1: In function ‘strncpy’, inlined from ‘main’ at wtf.c:21:5: /nix/store/fwpn2f7a4iqszyydw7ag61zlnp6xk5d3-glibc-2.30-dev/include/bits/string_fortified.h:106:10: warning: ‘__builtin_strncpy’ output may be truncated copying 100 bytes from a string of length 299 [-Wstringop-truncation] 106 | return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Expected Results: This example is expected to compile with no issues. Actual Results: The compiler erroneously warns about a truncated strcpy when no such truncation occurs.