I agree with David, it would make our life easier if a warning is triggered in such a case
Patrice Le 6 décembre 2011 09:04, David Brown <da...@westcontrol.com> a écrit : > On 05/12/2011 22:43, Jeff Law wrote: >> >> -----BEGIN PGP SIGNED MESSAGE----- >> Hash: SHA1 >> >> On 12/02/11 06:03, Patrice B wrote: >>> >>> Sorry for the noise, the problem is already tracked here: >>> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18501 >>> >>> Le 2 décembre 2011 10:42, Patrice Bouchand<pbfwdl...@gmail.com> a >>> écrit : >>>> >>>> Hello, >>>> >>>> I suspect a regression in uninitialized value detection, but >>>> before opening a bug I request your advices on the following >>>> problem: >>>> >>>> I compile the following code : >>>> ---------------------------------------------------- #include >>>> <stdio.h> #include<stdlib.h> >>>> >>>> int main( int argc, char **argv ) { int j; int rtn; int k,t; >>>> >>>> j = atoi( argv[1] ); >>>> >>>> if ( j> 5 ) { rtn = 10; } >>>> >>>> k=t; >>>> >>>> printf("rtn = %d\n", rtn); >>>> >>>> exit(0); } ---------------------------------------------------- >>>> >>>> With gcc 4.0: >>>> >>>> bash-4.2$ gcc-4.0 -O2 -Wall ./test_gcc2.c -o test_gcc >>>> ./test_gcc2.c: In function 'main': ./test_gcc2.c:17: warning: 't' >>>> is used uninitialized in this function ./test_gcc2.c:7: warning: >>>> 'rtn' may be used uninitialized in this function >>>> >>>> With gcc 4.6.1, the warning on rtn disappears : >>>> >>>> bash-4.2$ gcc -O2 -Wall ./test_gcc2.c -o test_gcc >>>> ./test_gcc2.c: In function ‘main’: ./test_gcc2.c:8:8: attention : >>>> variable ‘k’ set but not used [-Wunused-but-set-variable] >>>> ./test_gcc2.c:17:5: attention : ‘t’ is used uninitialized in this >>>> function [-Wuninitialized] >>>> >>>> >>>> Do I need to pass special options to gcc 4.6.1 to enable this >>>> detection or is it a gcc problem ? >> >> The uninitialized warning for rtn is not emitted because gcc-4.6.1 >> optimizes the code better than gcc-4.0. It effectively realizes the >> value is uninitialized on one path through the CFG while on the other >> path RTN has the value 10. >> >> When the uninitialized& initialized to 10 paths meet, the compiler >> >> (correctly) pretends the value for the uninitialized path is 10 as >> well. It then propagates the value 10 into the use of RTN in the >> printf statement. >> >> Thus by the time the uninitialized warning code runs all uses of the >> RTN variable have been eliminated leaving just a use of the constant >> 10. Thus you do not get a warning. >> >> jeff > > > That sounds to me like a bug. > > The point of a warning like "unintialised variable" is static error checking > - it is to help spot mistakes in your code. And if there is a path through > the function that uses an uninitialised variable, that's almost certainly a > bug in your code - one you would like the warning to tell you about. > > This is a case of the compiler seeing that there is undefined behaviour, and > picking it's own way to define that behaviour. It is of course free to do > that (you can implement undefined behaviour any way you want) - and picking > the shortest, fastest way is a good idea. But it should trigger the > warning. > > mvh., > > David >