The -Wstringop-truncation detects the potential string truncation in the first strncpy call below but fails to detect the similar problem in the second call. $ cat a.c && gcc -O2 -S -Wall a.c char a[2][7]; void f0 (const char *s) { __builtin_strncpy (a[0], s, sizeof a[0]); // missing -Wstringop-truncation } void f1 (const char *s) { __builtin_strncpy (a[1], s, sizeof a[1]); // -Wstringop-truncation (good) } void fi (const char *s, int i) { __builtin_strncpy (a[i], s, sizeof a[i]); // missing -Wstringop-truncation } a.c: In function ‘f1’: a.c:10:3: warning: ‘__builtin_strncpy’ specified bound 7 equals destination size [-Wstringop-truncation] __builtin_strncpy (a[1], s, sizeof a[1]); // -Wstringop-truncation (good) ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This is probably due to the same underlying problem as bug 84395. The checker computes the remaining space in the outermost object (via compute_objsize) instead of figuring out the size of the referenced array.