Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The following program compiles without warnings using gcc 4.7.0. The compiler uses the undefined behavior of accessing a variable that may not have been initialized to turn the conditional into one that's always taken, and then folds it away.

https://gist.github.com/2910012



valgrind:

   ==46592== Conditional jump or move depends on uninitialised value(s)
   ==46592==    at 0x100000E90: foo (in ./a.out)
   ==46592==    by 0x100000ED3: main (in ./a.out)
The matter is, valgrind exists. Everything is a C deficiency that valgrind is able to overcome decently is no longer a C deficiency practically speaking.


Not true. Valgrind is a dynamic analysis, while these kinds of errors could be caught by static analyses.


+1 I run valgrind on all my test cases.


clang does catch this:

  clang -Weverything uninit.c

  uninit.c:3:6: warning: no previous prototype for function 'foo' [-Wmissing-prototypes]
  void foo(int bar) {
     ^
  uninit.c:10:6: warning: variable 'lol' may be uninitialized when used here [-Wconditional-uninitialized]
        if (lol == 7) {
            ^~~
  uninit.c:4:9: note: initialize the variable 'lol' to silence this warning
        int lol;
               ^
                = 0
  uninit.c:37:3: warning: no newline at end of file [-pedantic,-Wnewline-eof]
  */
  ^

  3 warnings generated.


Glad to hear it--I've been looking into using clang more... This might just tip me towards it.


Interesting. Our compiler catches it, though:

  $ cparser -O3 -Wall -Wextra -pedantic test2.c -o uninit
  warning: ignoring gcc option '-pedantic'
  test2.c:3:6: warning: no previous declaration for 'void foo(int)' [-Wmissing-declarations]
  1 warning(s)
  test2.c:4:6: warning: 'variable lol' might be used uninitialized [-Wuninitialized]
Looking at the GCC bugtracker, there seem to be various warnings missing. http://gcc.gnu.org/bugzilla/buglist.cgi?quicksearch=uninitia...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: