Defensive Programming

by Mithrandir

Right now, I am reading “Defensive Programming for Red Hat Enterprise Linux (and What To Do If Something Goes Wrong)” a short article by Ulrich Drepper whenever I take a break from other activities from my busy schedule.

The book is full of cool and useful stuff but if you are not careful when using it and when your program is not so well designed you may end up with a segfault in valgrind (if you use valgring to chek for memory leaks as every programmer should do) or, better yet, with the following:

More than 10000000 total errors detected.  I'm not reporting any more.
 ==3725== Final error counts will be inaccurate.  Go fix your program!
 ==3725== Rerun with --error-limit=no to disable this cutoff.  Note
==3725== that errors may occur in your program without prior warning from
==3725== Valgrind, because errors are no longer being displayed.

In my case I was freeing a pointer and then printing it on the outside of the function. Not really freeing it, it was constructed with strndupa and it got freed by himself.

If you are not carefull the number of WTFs/min is growing.

About these ads