Warnings as errors

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andrew Davie

"Warnings as errors" has been my mantra for a long time.
Nonetheless, I did not enforce it ("-Werror") in the build, and I've just spent a couple of days dealing with the consequences of that.  I have a function 'drawGridPreview()' which was NOT called in the code that was locking-up/crashing, nor before it.  Just linked. And anything I touched in that function - simple or not - lead to a lockup/crash.  For example, changing "int trixelLine = 6;" to "int trixelLine = 10;" would trigger it. Or similar. It was completely non-comprehensible.

Eventually I found that disabling "-flto" switch in the link made the problem go away. That's an optimisation step taken by the linker. Even more bizarre. Why should changes in un-reachable code cause unrelated code to crash if the optimiser was enabled?

I dunno. Not really. But what I found out was that in that same function (drawGridPreview()) the compiler was warning me about a loop out of bounds condition. Looping 11 times instead of 10. So I fixed that (in non-running code!) and voila -- everthing running properly.  Furthermore, when it got to actually call drawGridPreview() it was all hunky dory, too.

The lesson here - be very very very very very careful about ignoring compiler warnings and thinking "that's minor, I'll fix that later; the code isn't called anyway!".  Biiig mistake, and I've learned my lesson. I've switched on "-Werror" and now fixed all my warnings. The irony here is that I often harp on about fixing warnings, and left this one and it bit me in the bum.

Also there were a few "variable masks other variable of same name" warnings, which turned out to be acutal bugs (for example, a for-i loop inside a for-i loop). These things are warned by the compiler, but I had a setup where these were not reported and/or I ignored them. And the code *seemed* to work.

So there we go.... add "-Werror" to your CFLAGS and whatever you do, treat warnings as errors.