how does -Wstringop-truncation work?

Patrick Herbst paherbst@gmail.com
Mon Jan 30 22:35:39 GMT 2023


I'm trying to understand how -Wstringop-truncation is generated.

I'm using the following example code with vanilla gcc 11.3 to generate
the warning

void append (char *buf, size_t bufsize)
{
  strncat (buf, ".txt", 3);
}

I can get the warning to display if running
gcc -Wall -02

I can even get it to display if running
gcc -Wall -O1 -foptimize-strlen

but i cannot get it to display if not using optimization.  I've even
tried manually adding all the -f options documented for -O1 to
understand what other flags might be needed to generate the warning.
I've had no success.

Is there a way to generate the -Wstringop-truncation warning without
optimization?


More information about the Gcc-help mailing list