https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82945
Bug ID: 82945 Summary: add warning for passing non-strings to functions that expect string arguments Product: gcc Version: 8.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- The -Wstringop-truncation warning new in GCC 8.0 warns about calls to strncpy and strncat that may non nul-terminate the copied string. Attribute nonstring was also added to make it possible to suppress the warning for character arrays that are not meant to be nul-terminated. However, using such arrays as arguments to functions that expect nul-terminated string arguments is accepted with no warning. To detect such misuses of the non-nul terminated arrays GCC should issue a warning when they are passed to string functions like strlen. $ cat c.c && gcc -O2 -S -Wall c.c char a[4]; int f (void) { __builtin_strncpy (a, "1234", 4); // -Wstringop-truncation (good) return __builtin_strlen (a); // because this is undefined } char b[4] __attribute__ ((nonstring)); int g (void) { __builtin_strncpy (b, "1234", 4); // no warning because of nonstring return __builtin_strlen (b); // but this is also undefined } c.c: In function ‘f’: c.c:5:3: warning: ‘__builtin_strncpy’ output truncated before terminating nul copying 4 bytes from a string of the same length [-Wstringop-truncation] __builtin_strncpy (a, "1234", 4); // -Wstringop-truncation ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~