Blog of me, Chris Idzerda, containing my thoughts, comments, and questions. RSS Feed


PC-lint Bug of the Month

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;
}

 
Posted by Chris Idzerda | 2 Comments | Trackback Url | Bookmark with:        
Tags:

Links to this Post

Comments

Friday, 2 Mar 2007 01:47 by Re: PC-lint Bug of the Month
Move the int previous = 'g'; outside of the for loop. Take that. - Scott S.

Sunday, 8 Jul 2007 12:09 by wait a second...
is this the blog of THE Chris Idzerda??? The genius? The guy who could code up a compiler in assembly while belching up a storm? The one that did toy story or bugs life or whatever it was years ago? The poly dude?

Name:
URL:
Email:
Comments:

CAPTCHA Image Validation