https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85700
Martin Sebor <msebor at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- Keywords| |diagnostic Status|UNCONFIRMED |NEW Last reconfirmed| |2018-05-08 CC| |msebor at gcc dot gnu.org Component|c++ |tree-optimization Ever confirmed|0 |1 --- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> --- Confirmed. Strncat is handled in maybe_diag_stxncpy_trunc() the same way as strncpy and the approach doesn't seem right for the former. The following test case shows the inconsistency. (The difference between C and C++ is due to the C <string.h> defining strncat as a macro which suppresses the warning due to -Wno-system-headers; in C++, strncat is defined as a function so -Wno-system-headers has no effect on calls to it.) $ cat u.c && gcc -O2 -S -Wall -ftrack-macro-expansion=0 /build/tmp/u.c #define strncat __builtin_strncat #define strlen __builtin_strlen char a[4], b[4]; void f1 (void) { strncat (a, "1", sizeof a - strlen (a) - 1); // no warning (good) } void f2 (void) { strncat (a, "12", sizeof a - strlen (a) - 1); // bogus -Wstringop-truncation } void fx (void) { strncat (a, b, sizeof a - strlen (a) - 1); // bogus -Wstringop-truncation } /build/tmp/u.c: In function ‘f2’: /build/tmp/u.c:13:3: warning: ‘__builtin_strncat’ output may be truncated copying between 0 and 3 bytes from a string of length 2 [-Wstringop-truncation] strncat (a, "12", sizeof a - strlen (a) - 1); // bogus -Wstringop-truncation ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /build/tmp/u.c: In function ‘fx’: /build/tmp/u.c:18:3: warning: ‘__builtin_strncat’ output may be truncated copying between 0 and 3 bytes from a string of length 3 [-Wstringop-truncation] strncat (a, b, sizeof a - strlen (a) - 1); // bogus -Wstringop-truncation ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~