Click on image to see enlargment

PC-lint/FlexeLint Output | Reference Manual Explanation | Home

            bug418.cpp

1    #include <stdio.h>
2    int count( const char * filename )
3        {
4        FILE *file = 0;
5        #define BUFLEN 300
6        char buf[BUFLEN];
7        int cnt = 0;
8        fopen( filename, "r" );
9        while( fgets( buf, BUFLEN, file ) )
10           cnt++;
11       fclose( file );
12       return cnt;
13       }
14   int main()   // Christmas count
15       {
16       printf( "I'll be getting %d presents\n",
17           count( "Sally's presents" ) );
18       return 0;
19       }

Little Sally wrote this program to try to get an idea of how many gifts she would be getting for Christmas, but there is one little flaw that her compiler did not catch. What is it?


bug418.cpp lint Output

--- Module:   bug418.cpp
                          _
    fopen( filename, "r" );
bug418.cpp  8  Warning 534: Ignoring return value of function 'fopen(const
    char *, const char *)' (compare with line 51, file c:\ansi\stdio.h)
                                      _
    while( fgets( buf, BUFLEN, file ) )
bug418.cpp  9  Warning 418: Passing null pointer to function 'fgets(char *,
    int, FILE *)', arg. no. 3 [Reference: file bug418.cpp: line 4]

bug418.cpp  9  Warning 668: Possibly passing a null pointer to function
    'fgets(char *, int, FILE *)', arg. no. 3 [Reference: file bug418.cpp: line 4]
                  _
    fclose( file );
bug418.cpp  11  Info 802: Conceivably passing a null pointer to function
    'fclose(FILE *)', arg. no. 1 [Reference: file bug418.cpp: line 4]

Reference Manual Explanation


418    Passing null pointer to function 'Symbol', Context Reference  --
       A NULL pointer is being passed to a function identified by Symbol.
       The argument in question is given by Context.  The function is
       either a library function designed not to receive a NULL pointer
       or a user function dubbed so via the option -function.
       See Function Mimicry

If you have comments or questions about this bug, please post them to our   Discussion Forum

Previous Bug - Bug #1510 - November 2005

[ new ] Use our Interactive Demo to Run FlexeLint on our Bugs of the Month

PC-lint/FlexeLint - Product Overview

Home | Contact | Order

PC-lint and FlexeLint are trademarks of Gimpel Software
Copyright 2006, Gimpel Software, All rights reserved.