https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97180
Martin Sebor <msebor at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- Last reconfirmed| |2020-09-23 CC| |msebor at gcc dot gnu.org Keywords| |diagnostic, | |missed-optimization Ever confirmed|0 |1 Component|c |tree-optimization Status|UNCONFIRMED |NEW Blocks| |83819 --- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> --- Below is a test case and GCC output for it (in your reports please be sure to provide full test cases as requested in https://gcc.gnu.org/bugs/#need). In this instance, the warning code knows the destination is the empty string, but it doesn't know the length of the source. It uses its size to compute the upper bound on its length. Since strncat(s1, s2, n) appends at most n nonzero bytes from s2 to s1 followed by a nul, when n is set to sizeof (s2) - 1 the functions copies the whole string and then adds the nul, with no truncation. So I agree that this is a bug in the warning. Thanks for reporting it! Independent of the warning, the strncat() call in this case (empty destination same size as source, bound == size - 1) can also be optimized to strcpy(). That would also avoid the warning. $ cat pr97180.c && gcc -O2 -S -Wall pr97180.c #define size 50 char char_table[size]; char char_table2[size]; void f (void) { __builtin_strcpy (char_table, ""); __builtin_strncat (char_table, char_table2, size - 1); } Referenced Bugs: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83819 [Bug 83819] [meta-bug] missing strlen optimizations