2

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?

DOOMDUDEMX
  • 644
  • 1
  • 8
  • 24
Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373
  • Oh, um, btw... There's GCC4 now... – Mysticial Mar 30 '12 at 07:38
  • 3
    Sheesh, how about fixing bugs in the compiler itself? That's an old version. (Not to mention language updates in general.) – GManNickG Mar 30 '12 at 07:59
  • 1
    Compiling your code with two or more different compilers can help to flush out numerous subtle problems, especially if you compile for different target architectures too. – Paul R Mar 30 '12 at 08:00
  • You might want to see if there are any new warning or error messages in gcc4 compared to gcc2, if there are any improvements or bug fixes w.r.t. warnings and errors and if there are any new compile options for warnings and errors. You should also note that gcc 2 came out before the C standard of 1999 and initially supported the C standard from 1989. You want to look at the differences between the two standards too as some things improved and, I think, some got deprecated. – Alexey Frunze Mar 30 '12 at 08:05
  • 2
    Lookup the bugs fixed between versions gcc2 to gcc4. All those bug fixes indirectly mean better bug discovery chances in user code. – Alok Save Mar 30 '12 at 08:06
  • Does your workplace have a large project that's built using GCC2? If so, can you simply take it as a test-case, and switch it to build against GCC4? – Oliver Charlesworth Mar 30 '12 at 08:14
  • @OliCharlesworth: That's what I am doing right now. Most errors are just because the code uses old syntax. If I fixed all the syntax errors, I would probably see other kinds of errors, and so on. But I don't consider those bugs, it is just syntax that will need to be converted. – Nicolas Raoul Mar 30 '12 at 08:18
  • 1
    Just keep in mind that switching to a newer compiler also means having to cope with new compiler bugs. GCC is typically plagued with bugs in the optimization code in the first versions of every new iteration so be careful with `-O2` and, especially, `-O3`. – HonkyTonk Mar 30 '12 at 08:28
  • If you're using C++, don't expect GCC 4 compiled binaries to link against GCC2 compiled binaries, and if they do link, don't expect them to run without crashing horribly. If you upgrade, you have to rebuild everything. – ams Mar 30 '12 at 08:58

2 Answers2

0

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 ...

Agnius Vasiliauskas
  • 10,935
  • 5
  • 50
  • 70
  • -finstrument-functions was already around in 2000... so it already exists in GCC2 I guess. http://gcc.gnu.org/ml/gcc-bugs/2000-06/msg00245.html – Nicolas Raoul Jun 12 '12 at 04:10
0

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
SoapBox
  • 20,457
  • 3
  • 51
  • 87
  • Could you please detail on warning/error messages? My question is about bug discovery, compile/run time and new features are not taken into consideration. Thanks a lot! – Nicolas Raoul Jun 12 '12 at 04:14
  • I don't have access to gcc2 to say for certain and I know more about C++ than C. Your best bet would be to look at the changelogs between GCC versions and then try out a few basic things in both to see the difference. One thing that falls under runtime checking: GCC3 introduced runtime stack checking, where some types of stack corruption can be caught at runtime (and result in an `abort()`). – SoapBox Jun 12 '12 at 09:43