From: David Brown <david@westcontrol.com>
To: gcc-help@gcc.gnu.org
Subject: Re: Question about declaring an array in the stack on runtime
Date: Mon, 17 Jul 2023 14:45:11 +0200 [thread overview]
Message-ID: <u93d4n$hfv$1@ciao.gmane.io> (raw)
In-Reply-To: <CAA_Li+tJ3DW_65naXfZKHERJpJExbdfwsoJDzQDXYBgiEvanKA@mail.gmail.com>
On 15/07/2023 12:43, James R T via Gcc-help wrote:
> Hi folks,
>
> 2. Regardless of whether this is UB or not, is it possible for GCC to
> also output a warning in `-O0` as in `-O2`? If the behavior changes
> across different optimization levels, it seems that it's worth a
> warning or two. It can be a different warning instead of
> `-Wdangling-pointer` since looking at the produced assembly code, GCC
> seems to simply optimize out the whole conditional assignment block in
> `-O2`. If it is UB, I understand that it is impossible to catch all
> UB, but I am just checking on whether it is possible to catch this
> specific one from GCC's perspective. Just FYI, I have also tried using
> `-fsanitize=address` and `-fsanitize=undefined` and it seems that
> AddressSanitizer would throw a `stack-use-after-scope` error in GCC if
> `-fsanitize=address` is specified for both `-O0` and `-O2`, but not in
> Clang. `-fsanitize=undefined` does not seem to be able to detect
> anything.
>
I think you've had solid answers to your other points already, so you
can see why it is UB.
When you use the -O flags, you are not just enabling a selection of
optimisation passes - you are also changing the code analysis passes.
With -O0, relatively little analysis is done. This means that many
warnings are not active at -O0. So it is quite normal for warnings like
this to require -O1 or above to work, and some may need -O2 to work
fully (I don't have details off-hand - I'm a long-term gcc user, not a
gcc developer).
Personally, I've never had any use for -O0 - I use -O2 usually and -O1
at a minimum, even for early checking of code, precisely because it
allows far better static warnings.
prev parent reply other threads:[~2023-07-17 12:45 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-15 10:43 James R T
2023-07-15 10:49 ` John Scott
2023-07-15 11:19 ` Xi Ruoyao
2023-07-15 12:57 ` James R T
2023-07-17 12:45 ` David Brown [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='u93d4n$hfv$1@ciao.gmane.io' \
--to=david@westcontrol.com \
--cc=gcc-help@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).