[Bug tree-optimization/101451] Incorrect -Wstringop-truncation warning

quentin at armitage dot org.uk gcc-bugzilla@gcc.gnu.org
Thu Aug 12 21:05:48 GMT 2021


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101451

--- Comment #5 from Quentin Armitage <quentin at armitage dot org.uk> ---
In the code of my original example (which I have simplified)

===================================
#include <string.h>

static char dest[16];
static char src[16] = "012345678901234";

int main(__attribute__(void)
{
  do { dest[0] = '\0'; strncat(dest, src, sizeof(dest) - 1); } while (0);
}
==================================

there is no truncation since 15 bytes are copied to dest, with a NUL byte
added, and the maximum length of a string in src is 15 bytes plus terminating
NUL byte.

Also, the warning is not generated if -O2 is not specified, but it is generated
if -O2 is specified (-O1 does not generate the warning). To me this doesn't
seem right.


More information about the Gcc-bugs mailing list