This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Dubious "'foo' might be used uninitialized in this function" message


When compiling the following program,

#include <unistd.h>

int f (int x, int y)
{
 int z;

 if (x)
   z = getppid ();
 y = getpid ();
 if (x)
   y += z;
 return x + y + z;
}

GCC (with '-Wall') always says:

w.c: In function `f':
w.c:5: warning: 'z' might be used uninitialized in this function

which is not true.

Here 'z' is initialized under 'if (x)' condition, and 'z' always used under
'if (x)' condition. Also, it's clear that 'x' isn't accessed between 'if (x)',
so it's impossible to access uninitialized 'z'.


Is it reasonable to learn GCC do more analysis in attempt to avoid
warning in this case ? How is it complex ?

Also, IMHO it would be better to point to the place where
uninitialized variable may be _used_ and not to the place where
it's _declared_ ('y += z' in this case). Or even to both places.

For example, for the following program:

#include <unistd.h>

int f (int x, int y)
{
 int z;

 if (x)
   z = getppid ();
 y = getpid ();
 y += z;
 return x + y + z;
}

it would be better to get:

w.c: In function `f':
w.c:10: warning: uninitialized 'z' might be used
w.c:5: warning: ('z' declared here)

Thanks,
Dmitry


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]