By clandau

2013-07-17 16:59:22 8 Comments

The following code has a variable that may be uninitialized. It seems that gcc should be generating a warning but isn't:

$ cat a.c
int foo(int b) {
  int a;
  if (b)
    a = 1;
  return a;

$ gcc-4.7 -c -Wall -Wmaybe-uninitialized -o a.o ./a.c
$ gcc-4.7 -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.3-2ubuntu1~12.04' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --with-system-zlib --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.7.3 (Ubuntu/Linaro 4.7.3-2ubuntu1~12.04)

Any clues on how to get gcc to report the uninitialized variable?


@Spidey 2016-05-03 22:49:02

You need to enable optimizations of some kind. Recompile your example with -O2, for example, and the warning will appear.

Since this requires analysis of code paths and it's a rather expensive computation, GCC only enables it when asked to optimize code.

@Matt Reyer 2017-10-11 20:38:10

This worked for me. Unfortunate that I can't get this behavior without -O2.

@ewh 2014-05-06 10:06:37


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

int main(void)
   int result;
      result = 1;

   printf("%d\n", result);

   return 0;

Here are several test-runs:

$ avr-gcc -Os -Wuninitialized -o uninitializedTest uninitializedTest.c 
$ gcc-4.2 -Os -Wuninitialized -o uninitializedTest uninitializedTest.c 
$ gcc-4.0 -Os -Wuninitialized -o uninitializedTest uninitializedTest.c 
uninitializedTest.c: In function ‘main’:
uninitializedTest.c:32: warning: ‘result’ may be used uninitialized in this function

$ gcc-4.2 -Os -Wmaybe-uninitialized -o uninitializedTest uninitializedTest.c 
cc1: error: unrecognized command line option "-Wmaybe-uninitialized"
$ gcc-4.2 -Os -Wuninitialized -o uninitializedTest uninitializedTest.c 
$ gcc-mp-4.8 -Os -Wuninitialized -o uninitializedTest uninitializedTest.c 
$ gcc-mp-4.8 -Os -Wmaybe-uninitialized -o uninitializedTest uninitializedTest.c 

avr-gcc is 4.8 (Also undetected with avr-gcc-4.4.5)

It appears gcc-4.0 had the ability to recognize the problem.

$ gcc-4.0 -v
Configured with: /var/tmp/gcc/gcc-5493~1/src/configure --disable-checking -enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.0/ --with-gxx-include-dir=/include/c++/4.0.0 --with-slibdir=/usr/lib --build=i686-apple-darwin9 --program-prefix= --host=powerpc-apple-darwin9 --target=powerpc-apple-darwin9
Thread model: posix
gcc version 4.0.1 (Apple Inc. build 5493)

So... was it an Apple-specific addition, that they hold license to? Or maybe that's why Apple's been moving away from open-source... Could it be they fixed a decade-old bug and the gcc community didn't accept it?

@Carl Norum 2013-07-17 17:07:58

It looks like you can't - see this bug report. (And this one, which is marked as a dupe of that one - it has an identical test case to yours.) Since it looks like the root-cause bug is almost 10 years old, it would seem it's not an easy problem to solve. In fact, the second bug I linked to has the phrase "Never going to be fixed" in the discussion, so that doesn't look good.

If it's really important to you, clang does catch this one with -Wsometimes-uninitialized, which is included with -Wall:

a.c:3:7: warning: variable 'a' is used uninitialized whenever 'if' condition is
      false [-Wsometimes-uninitialized]
  if (b)
a.c:5:10: note: uninitialized use occurs here
  return a;
a.c:3:3: note: remove the 'if' if its condition is always true
  if (b)
a.c:2:8: note: initialize the variable 'a' to silence this warning
  int a;
        = 0
1 warning generated.

@clandau 2013-07-17 23:07:51

My clang (version 3.0) recognizes neither -Wsometimes-uninitialized nor -Wmaybe-uninitialized. I was able to generate the warnings displayed above with -Wconditional-uninitialized or -Weverything.

@Carl Norum 2013-07-17 23:42:20

Clang 3.0 is pretty old.

@R.. 2013-07-17 17:17:40

The problem is that gcc can't know you'll call the function with a zero argument. In this case, the fact that you're testing might be a good hint that you intend to sometimes do so, but the general case is much harder. Consider:

int foo(int b) {
  int a;
  switch(b) {
  case 1:
    a = 1;
  case 2:
    a = 0;
  case 3:
    a = 2;
  return a;

This would be a perfectly reasonable function whose interface contract is that you only pass 1, 2, or 3 to it, and any "uninitialized" warning would in that case be spurious, and thereby reduce the signal-to-noise ratio of the compiler's warning generation.

I agree it would be nice if compilers gave better diagnostics for things like this, but it's not easy, and warnings that can have false-positives are always a delicate balancing act between cluttering code with workarounds for the warnings and failing to catch bugs.

@clandau 2013-07-17 21:55:33

This is exactly the example given in the gcc manual at the entry for -Wuninitialized. As it says there, "these warnings are made optional" for that reason. Apparently, that is wrong, and there is no option to get the warning.

@Craig McQueen 2014-05-29 07:26:37

In these cases, I'd happily accept a false positive, aka spurious warning, especially when I've explicitly turned on -Wuninitialized or -Wall. Much preferable than all the false negatives currently (i.e. no warning when there should be one). Arguably, if a compiler can't figure it out reliably, then neither can a human reliably, so the variable ought to have an intialiser.

Related Questions

Sponsored Content

3 Answered Questions

3 Answered Questions

[SOLVED] GCC: sorry, unimplemented: 64-bit mode not compiled in

  • 2012-01-14 08:41:02
  • Martin
  • 40170 View
  • 26 Score
  • 3 Answer
  • Tags:   c++ gcc

12 Answered Questions

1 Answered Questions

How to link gfortran when I compile?

0 Answered Questions

Building HHVM missing GCC 4.7 but it is installed

  • 2014-03-20 19:04:15
  • Alex
  • 183 View
  • 2 Score
  • 0 Answer
  • Tags:   ubuntu gcc hhvm

4 Answered Questions

[SOLVED] program works only when an integer is declared

  • 2014-01-26 11:17:02
  • cyberpirate92
  • 94 View
  • 0 Score
  • 4 Answer
  • Tags:   c linux gcc

2 Answered Questions

[SOLVED] Can't compile C++ file.cpp. C++98 mode

2 Answered Questions

[SOLVED] Run time error in C program in ubuntu. But executed in well SunOS

1 Answered Questions

[SOLVED] How to install a specific version of gcc

  • 2012-04-07 18:02:58
  • Don Wool
  • 2090 View
  • 2 Score
  • 1 Answer
  • Tags:   linux gcc

1 Answered Questions

[SOLVED] DSO's dynamic symbol table has more entries under 64-bit GCC than 32-bit GCC

Sponsored Content