public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* how does -Wstringop-truncation work?
@ 2023-01-30 22:35 Patrick Herbst
  2023-01-30 23:09 ` Segher Boessenkool
  2023-01-30 23:36 ` Jonathan Wakely
  0 siblings, 2 replies; 4+ messages in thread
From: Patrick Herbst @ 2023-01-30 22:35 UTC (permalink / raw)
  To: gcc-help

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?

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: how does -Wstringop-truncation work?
  2023-01-30 22:35 how does -Wstringop-truncation work? Patrick Herbst
@ 2023-01-30 23:09 ` Segher Boessenkool
  2023-01-30 23:16   ` Patrick Herbst
  2023-01-30 23:36 ` Jonathan Wakely
  1 sibling, 1 reply; 4+ messages in thread
From: Segher Boessenkool @ 2023-01-30 23:09 UTC (permalink / raw)
  To: Patrick Herbst; +Cc: gcc-help

On Mon, Jan 30, 2023 at 05:35:39PM -0500, Patrick Herbst via Gcc-help wrote:
> 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.

The manual says
  The effectiveness of some warnings depends on optimizations also being
  enabled.  For example '-Wsuggest-final-types' is more effective with
  link-time optimization and some instances of other warnings may not be
  issued at all unless optimization is enabled.  While optimization in
  general improves the efficacy of control and data flow sensitive
  warnings, in some cases it may also cause false positives.

> 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.

Flags like -O1 do more than just enabling some other flags.


Segher

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: how does -Wstringop-truncation work?
  2023-01-30 23:09 ` Segher Boessenkool
@ 2023-01-30 23:16   ` Patrick Herbst
  0 siblings, 0 replies; 4+ messages in thread
From: Patrick Herbst @ 2023-01-30 23:16 UTC (permalink / raw)
  To: Segher Boessenkool; +Cc: gcc-help

On Mon, Jan 30, 2023 at 6:12 PM Segher Boessenkool
<segher@kernel.crashing.org> wrote:
>
> The manual says
>   The effectiveness of some warnings depends on optimizations also being
>   enabled.  For example '-Wsuggest-final-types' is more effective with
>   link-time optimization and some instances of other warnings may not be
>   issued at all unless optimization is enabled.  While optimization in
>   general improves the efficacy of control and data flow sensitive
>   warnings, in some cases it may also cause false positives.
>
>
> Flags like -O1 do more than just enabling some other flags.
>

Thank you!
Another point of clarification; how is it that -foptimize-strlen is
necessary for -Wstringop-truncation to work?  Is that noted somewhere?

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: how does -Wstringop-truncation work?
  2023-01-30 22:35 how does -Wstringop-truncation work? Patrick Herbst
  2023-01-30 23:09 ` Segher Boessenkool
@ 2023-01-30 23:36 ` Jonathan Wakely
  1 sibling, 0 replies; 4+ messages in thread
From: Jonathan Wakely @ 2023-01-30 23:36 UTC (permalink / raw)
  To: Patrick Herbst; +Cc: gcc-help

On Mon, 30 Jan 2023 at 22:36, Patrick Herbst via Gcc-help
<gcc-help@gcc.gnu.org> wrote:
> 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.

Because you can't enable optimization with -f options.

See https://gcc.gnu.org/wiki/FAQ#optimization-options

If you don't use one of the -Ox optimization options then **no**
optimization passes run at all, so enabling and disabling individual
optimization passes with -fxxx options has no effect, They're just
skipped completely.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-01-30 23:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-30 22:35 how does -Wstringop-truncation work? Patrick Herbst
2023-01-30 23:09 ` Segher Boessenkool
2023-01-30 23:16   ` Patrick Herbst
2023-01-30 23:36 ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).