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

            Bug ID: 98049
           Summary: False positive when compiling with -Os
                    -Wmaybe-uninitialized
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ahmad at a3f dot at
  Target Milestone: ---

Created attachment 49642
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=49642&action=edit
C Source reproducing the erroneous warning

gcc -Os warns that "'start' may be used uninitialized in this function
[-Wmaybe-uninitialized]" in this snippet:

unsigned start;

if (timeout)
        start = get_time();
while (!active()) {
        if (timeout && is_timeout(start, timeout))
                       ~~~~~~~~~~^~~~~~~~~~~~~~~~
                return 1;
}

This is wrong, because `start' is only evaluated when `timeout' != 0
at which time it's always initialized. `timeout' is automatic storage and can't
change in-between.

The warning vanishes if -Os is removed or code around is moved.

Steps to reproduce (or check https://godbolt.org/z/3qf58T):

$ cc minim.c -c -Os -Wmaybe-uninitialized

<source>: In function 'void f(int, unsigned int)':
<source>:12:42: warning: 'start' may be used uninitialized in this function
[-Wmaybe-uninitialized]
   12 |                 if (timeout && is_timeout(start, timeout))
      |                                ~~~~~~~~~~^~~~~~~~~~~~~~~~
<source>:7:18: note: 'start' was declared here
    7 |         unsigned start;
      |                  ^~~~~

Reply via email to