[Bug c/98871] New: Cannot silence -Wmaybe-uninitialized at declaration site

1zeeky at gmail dot com gcc-bugzilla@gcc.gnu.org
Thu Jan 28 21:44:34 GMT 2021


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


More information about the Gcc-bugs mailing list