public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/98871] New: Cannot silence -Wmaybe-uninitialized at declaration site
@ 2021-01-28 21:44 1zeeky at gmail dot com
  2021-01-28 21:46 ` [Bug c/98871] " 1zeeky at gmail dot com
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: 1zeeky at gmail dot com @ 2021-01-28 21:44 UTC (permalink / raw)
  To: gcc-bugs

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

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

end of thread, other threads:[~2021-11-12 19:44 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-28 21:44 [Bug c/98871] New: Cannot silence -Wmaybe-uninitialized at declaration site 1zeeky at gmail dot com
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

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