PC-lint is a static analysis tool for C and C++ that's been around for 20 years. Gimpel Software, the makers of PC-lint, has advertised it with a small C program that has a bug. I like to solve them before checking the site to verify my solution. Here is bug #774 from the December issue of Dr. Dobb's Journal. If you use Visual C++ to compile it, it won't catch the error, even if you enable all warnings. Jeff, who pestered me to make this post, managed to solve it even though it's unmanaged code.
#include <stdio.h> char Cheryl[] = "gbgbbgbbgggb"; int main() { char *p; int naughty = 0; for( p = Cheryl; *p; p++ ) { int previous = 'g'; /* previous behavior was 'g' */ if( *p == 'b' && previous == 'b' ) naughty++; else if( *p == 'b' ) previous = 'b'; else previous = 'g'; } if( naughty ) printf( "Cheryl has been naughty\n" ); return 0; }
