public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "1zeeky at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/98871] New: Cannot silence -Wmaybe-uninitialized at declaration site
Date: Thu, 28 Jan 2021 21:44:34 +0000	[thread overview]
Message-ID: <bug-98871-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 98871
           Summary: Cannot silence -Wmaybe-uninitialized at declaration
                    site
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 1zeeky at gmail dot com
  Target Milestone: ---

Created attachment 50077
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=50077&action=edit
testcase

I have some code where I'm relying on the caller to uphold some guarantee in
order to properly initialize a variable.
gcc correctly detects that the variable maybe left uninitialized. The warning
is correct, but I am unable to properly silence this warning because the
warning doesn't "show up" at the location where the variable is declared, but
where it is used, which is another function, which receives it via pointer.

This is probably better explained with the testcase. The setup is a bit weird
and looks like it could be minimized. I needed to increase the complexity to
thwart static analysis sufficiently such that gcc is unsure if 'max' gets
initialized or not.
Here's the warning I get:

$ gcc -O -Wmaybe-uninitialized maybe-uninit.c
maybe-uninit.c: In function ‘main’:
maybe-uninit.c:8:9: warning: ‘max.x’ may be used uninitialized in this function
[-Wmaybe-uninitialized]
    8 |  return ++*x;
      |         ^~~~
maybe-uninit.c:15:13: note: ‘max.x’ was declared here
   15 |  struct Foo max;
      |             ^~~

A site-note: the first line in the error-report states "In function ‘main’" for
some reason.

I would like to silence the warning from within foo() (my failed attempt at
using '#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"' is left in the
testcase), since inc() is used in other places where having this warning may be
beneficial.
The warning would probably have to change to something like

maybe-uninit.c: In function ‘foo’:
maybe-uninit.c:22:13: warning: 'max.x' is passed to function 'inc', where it
may be used uninitialized [-Wmaybe-pass-uninitialized]
   22 |  return inc(&max.x);
      |             ^~~~~~
maybe-uninit.c:8:9: note: ‘max.x’ used here
    8 |  return ++*x;
      |         ^~~~
maybe-uninit.c:15:13: note: ‘max.x’ was declared here
   15 |  struct Foo max;

Alternatively, a variable attribute would work for me as well, e.g.
'__attribute__((assume_initialized))'.

Locally I use gcc 10.2.0, but through godbolt.org I also tested 10.2.1 and "gcc
(trunk)" which describes itself as "gcc (Compiler-Explorer-Build) 11.0.0
20210127 (experimental)".

             reply	other threads:[~2021-01-28 21:44 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-28 21:44 1zeeky at gmail dot com [this message]
2021-01-28 21:46 ` [Bug c/98871] " 1zeeky at gmail dot com
2021-01-29  0:49 ` [Bug middle-end/98871] " msebor at gcc dot gnu.org
2021-01-29  0:59 ` msebor at gcc dot gnu.org
2021-01-29 21:56 ` msebor at gcc dot gnu.org
2021-01-29 21:56 ` msebor at gcc dot gnu.org
2021-06-10 23:31 ` msebor at gcc dot gnu.org
2021-07-02 22:20 ` cvs-commit at gcc dot gnu.org
2021-07-06 19:43 ` cvs-commit at gcc dot gnu.org
2021-11-12 19:44 ` msebor 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-98871-4@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).