Examples showing how switching to a modern C compiler can help discover bugs?

220 Views Asked by At

I am preparing a note to convince people that switching from GCC2 to GCC4 (as a C compiler) is a good idea.

In particular, I think it can reveal existing bugs. I would like to give examples, but as a Java programmer my experience of this situations is limited. One example is return type checking, I guess.

What are other convincing examples showing that switching to a modern compiler can help discover bugs that exist in C code?

2

There are 2 best solutions below

1
On

Well, some gcc options which is very useful in bugs discovery:

  • -finstrument-functions - helps to build function call stack tracer. Especially on architectures where built-in __builtin_return_address() scope is limited only to current function at hand. Stack tracer together with linker's symbol file generated with -Map linker option are indispensable tools for detecting memory leaks (suppose you develop embedded system on which Valgrind can't be run or etc.)
  • -fstack-protector-all is very useful for detecting where code writes bytes to memory in out-of-buffer place. So this option detects buffer-overflow type bugs.

Errr... just those two options are in mind. Possibly there are more which I don't know ...

2
On

I assume these people have a particular piece of code they're using gcc2 with. The best thing to do might be to just take that code and compile it in gcc4 with all possible warnings turned on and compare the difference.

Some other differences between gcc2 and gcc4 are likely to be:

  • Better compile times (gcc4 is probably faster)
  • Much better code run times (gcc4 is better at optimizing, and has knowledge of CPU architecture that did not exist when gcc2 came out).
  • Better warning/error messages
  • I'm sure there are some interesting new GNU C extensions in gcc4