public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "avarab at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug analyzer/105273] -Wanalyzer-use-of-uninitialized-value warns on "missing" default for switch when callers can be statically determined
Date: Thu, 14 Apr 2022 11:37:24 +0000	[thread overview]
Message-ID: <bug-105273-4-PwflxYv4xK@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-105273-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #2 from Ævar Arnfjörð Bjarmason <avarab at gmail dot com> ---
...To finish the report (Bugzilla's eager submitting threw me for a loop) the
issue is that while the analyzer is right in the *general* case about a
"switch" with a missing "default" being something that *could* be fed any
arbitrary value, in this case all of the possible values can be determined at a
compile-time.

Which is all this bug report is suggesting as an initial report, that it would
be nice to have that narrow case handled.

END OF NARROW REPORT

More generally though (and perhaps I should submit another report for this)
it's a really useful feature of GCC (and clang) that with C they put a bit of
trust in the programmer with -Wswitch (which is enabled under -Wall).

Because even though there are cases where the programmer is wrong and
exhaustively enumerating the enum labels isn't sufficient, in the general case
being able to avoid "default" cases in favor of exhaustively listing the labels
avoids a *lot* of subtle bugs in larger codebases.

That's because the values being thrown around to "switch" on are validated
already by [insert magic here], but whenever *new* values are added it's really
important to not miss 1/N switch statements that new labels need to be added
to.

In the testcase for this bug the compiler has enough visibility to determine
this to be true without the "[insert magic here]", but in cases where that's
not true it seems to me that those users -fanalyzer would be encouraged to add
"default" cases just to appease the compiler, and thus get worse warnings from
-Wswitch.

I may be missing something obvious, but it would be nice to have some way out
of that where you can have your cake & eat it too. I.e. only have -fanalyze
complain about this class of issue where -Wswitch would complain, and have the
current behavior in GCC 12.0 hidden behind some opt-in sub-flag of
-Wanalyzer-use-of-uninitialized-value.

Anyway, just my 0.02. Thanks a lot for -fanalyze, I've been trying it out on
the git codebase and it's uncovered a lot of genuine issues already. I'm just
filing some bugs for the long tail where the analyzer seemed to be
wrong/lacking. Thanks!

  parent reply	other threads:[~2022-04-14 11:37 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-14 11:23 [Bug analyzer/105273] New: " avarab at gmail dot com
2022-04-14 11:25 ` [Bug analyzer/105273] " avarab at gmail dot com
2022-04-14 11:37 ` avarab at gmail dot com [this message]
2022-04-14 17:28 ` egallager at gcc dot gnu.org
2022-04-14 21:04 ` dmalcolm at gcc dot gnu.org
2023-01-12 20:06 ` dmalcolm at gcc dot gnu.org
2023-01-12 21:05 ` dmalcolm at gcc dot gnu.org
2023-01-13 22:52 ` cvs-commit at gcc dot gnu.org
2023-01-13 22:56 ` dmalcolm at gcc dot gnu.org
2023-03-29 23:36 ` dmalcolm at gcc dot gnu.org

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=bug-105273-4-PwflxYv4xK@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).