3

gcc version - gcc (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4

Compiling below code with -Wall -Wuninitialized is throwing warning as expected warning: ‘test’ is used uninitialized in this function [-Wuninitialized]

#include<stdio.h>
#include<stdlib.h>

void check (int test )
{
   printf("%d",test);
}

int
main()
{
   int test;
   check(test);
   return 0;
}

But compiling below code with -Wall -Wuninitialized is not throwing warning

#include<stdio.h>
#include<stdlib.h>

void check (int test )
{
   printf("%d",test);
}

int
main()
{
   int test;
   int condition = 0;

   if(condition == 27)
      test = 10;

   check(test);
   return 0;
}

Shouldn't it throw warning?. is it anyway related to compiler optimization?

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
user1762571
  • 1,888
  • 7
  • 28
  • 47
  • There is no requirement by the standard a compiler has to warn about this type of undefined behaviour. – too honest for this site Mar 09 '16 at 00:44
  • test = 10; I guess it takes the variable as used, even if there's a path unitialized. – xvan Mar 09 '16 at 00:44
  • [Related thread](http://stackoverflow.com/questions/17705880/gcc-failing-to-warn-of-uninitialized-variable?rq=1) – M.M Mar 09 '16 at 00:48
  • According to [here](http://goo.gl/jhufUK), gcc does warn about this since version 4.9 . You could upgrade your compiler version perhaps. – M.M Mar 09 '16 at 00:50
  • @M.M I have 4.9.3 and 5.3.0 no warning even with all those flags. – Schwern Mar 09 '16 at 00:59
  • @Schwern there is a dropdown box where you select the compiler version. I linked to version 4.9.0 but you can change it. Versions 4.9.0, 5.1.0, 5.2.0, 5.3.0, and 6, all give the warning. Version 4.9.2 does not. (I didn't try varying the `-O` flag) – M.M Mar 09 '16 at 01:08
  • @M.M Yeah, I see that. I can't replicate it. *shrug* – Schwern Mar 09 '16 at 01:27

2 Answers2

2

What an user understands as a false positive may be different for the particular user. Some users are interested in cases that are hidden because of actions of the optimizers combined with the current environment. However, many users aren't, since that case is hidden because it cannot arise in the compiled code. The canonical example is (MM05):

int x;
if (f ())
x = 3;
return x;

where 'f' always return non-zero for the current environment, and thus, it may be optimised away. Here, a group of users would like to get an uninitialized warning since 'f' may return zero when compiled elsewhere. Yet, other group of users would consider spurious a warning about a situation that cannot arise in the executable being compiled.

https://gcc.gnu.org/wiki/Better_Uninitialized_Warnings#Proposal


EDIT: MMO5 is already fixed

https://gcc.gnu.org/wiki/Better_Uninitialized_Warnings#MM05

xvan
  • 4,554
  • 1
  • 22
  • 37
1

Yes, gcc is not warning you because the code is optimized away. Some people think this is the right thing, because it might be that way by a compile time configuration value. Others think it's the wrong thing, because it hides possible mistakes.

You are not the first to notice this problem with GCC. There's a whole proposal about it.

clang with -Wall detects the problem and even suggests a fix.

$ make
cc -Wall -g    test.c   -o test
test.c:15:7: warning: variable 'test' is used uninitialized whenever 'if' condition is false
      [-Wsometimes-uninitialized]
   if(condition == 27)
      ^~~~~~~~~~~~~~~
test.c:18:10: note: uninitialized use occurs here
   check(test);
         ^~~~
test.c:15:4: note: remove the 'if' if its condition is always true
   if(condition == 27)
   ^~~~~~~~~~~~~~~~~~~
test.c:12:12: note: initialize the variable 'test' to silence this warning
   int test;
           ^
            = 0
1 warning generated.
Schwern
  • 153,029
  • 25
  • 195
  • 336