Click on image to see enlargment

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

            bug534.c

1    #include <stdio.h>
2    #include <malloc.h>
3    #include <string.h>
4
5    char *obtain_data()
6        {
7        char *buf = malloc( 100 );
8        FILE *f;
9        size_t length = 0;
10
11       if( !buf ) return NULL;
12       f = fopen( "football.dat", "r" );
13       if( !f ) return NULL;
14       *buf = '\0';
15       while( fgets( buf+length, 100, f ) )
16           {
17           length = strlen(buf);
18           realloc( buf, length + 100 );
19           }
20       (void)fclose( f );
21       return buf;
22   }

Alex is automating his fantasy football lineup changes and learning C at the same time. In reading his data into memory, he is making a critical mistake. Can you find it?


bug534.c lint Output

--- Module:   bug534.c (C)
                    _
    if( !f ) return NULL;
bug534.c(13) : Warning 429: Custodial pointer 'buf' (line 7) has not been freed or returned 
                                    _
        realloc( buf, length + 100 );
bug534.c(18) : Warning 534: Ignoring return value of function 'realloc(void *, unsigned int)'
    (compare with line 108, file c:\compiler\include\malloc.h)

bug534.c(15) : Warning 449: Pointer variable 'buf' previously deallocated
    [Reference: file bug534.c: line 18]

Reference Manual Explanation


534    Ignoring return value of function 'Symbol' (compare with Location)  -- A function
       that returns a value is called just for side effects as, for example, in a statement
       by itself or the left-hand side of a comma operator.  Try: (void) function(); to
       call a function and ignore its return value.  See also the fvr, fvo and fdr flags.


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


Previous Bug - Bug #1529 - September 2012

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 2012, Gimpel Software, All rights reserved.