leah blogs

April 2004

05apr2004 · Abuse of assert(3)

Yesterday I saw some awful abuse of assert(3). First:

 file = fopen ("...");
 assert (file);

Repeat after me: assert is not for userlevel errors! Instead it's purpose is to find internal inconsistencies. However, then the real fun started:

 assert (strcpy (dest, src));

Hell, no! assert is a macro, if you compile with -DNDEBUG, you get serious problems. Even the manpage says that:

BUGS: assert() is implemented as a macro; if the expression tested has side-effects, program behaviour will be different depending on whether NDEBUG is defined. This may create Heisenbugs which go away when debugging is turned on.

But then, the developer programmed that in Visual C++...

Copyright © 2004–2018