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

            Bug ID: 81897
           Summary: spurious -Wmaybe-uninitialized warning
           Product: gcc
           Version: 7.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: arnd at linaro dot org
  Target Milestone: ---

I keep seeing a class of false-positive -Wmaybe-uninitialized in the linux
kernel and have done countless kernel patches to work around them. The latest
one triggered me to look a little deeper. The general pattern I see is that
when one variable is conditionally initialized and conditionally used based on
the same variable, but any form of lock (mutex, semaphore, spinlock, ...)
release happens in-between, the compiler no longer knows if the initialization
was correct, and produces a spurious warning.

This particular instance of the warning I saw today reduced to a relatively
simple function between the initialization and the use:

int f(void);
static inline void rcu_read_unlock(void)
{
        static _Bool __warned;
        if (f() && !__warned && !f()) {
                __warned = 1;
        }
}
int inet6_rtm_getroute(void)
{
        int dst;
        int fibmatch = f();

        if (!fibmatch)
                dst = f();
        rcu_read_unlock();
        if (fibmatch)
                dst = 0;

        return dst;
}

Clearly, 'dst' is initialized here when it gets returned.

I can reproduce the warning in gcc-4.7 through 7.1.1, which is the latest I
tried.

Reply via email to