E:\>type blam.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct pgmimage {
char *magic_str; /* File identifier */
int nr, nc; /* Rows and columns in the image */
int max_val; /* Max value of data */
int or, oy; /* Origin */
unsigned char **data; /* Pixel values */
};
int main ( int argc, char *argv[])
{
FILE *fp;
char line_in[3000];
int count = 0;
int row, col;
struct pgmimage *pgm_in;
if ( argc < 2 )
{
printf ("You need to enter a file name\n");
printf ("\n");
printf ("Usage: %s <file_name>\n", argv[0]);
return 1;
}
fp = fopen( argv[1], "r");
if (fp == NULL)
{
printf (" unable to open file: %s\n", argv[1]);
return 1;
}
pgm_in = malloc(sizeof *pgm_in);
pgm_in->magic_str = malloc(10 * sizeof *pgm_in->magic_str);
while( fgets( line_in, 2999, fp) != NULL)
{
if (line_in[0] != '#')
{
count = count + 1;
if (count == 1)
{sscanf(line_in, "%s", &pgm_in->magic_str);}
if (count == 2)
{sscanf(line_in, "%i %i", &pgm_in->nr, &pgm_in->nc);}
if (count == 3)
{sscanf(line_in, "%i", &pgm_in->max_val); break;}
}
}
printf("PGM Header: Magic # %s\n", &pgm_in->magic_str);
printf("PGM Header: Rows # %i\n", pgm_in->nr);
printf("PGM Header: Columns # %i\n", pgm_in->nc);
printf("PGM Header: Max Value # %i\n", pgm_in->max_val);
pgm_in->data = malloc( pgm_in->nc * pgm_in->nr * sizeof *pgm_in->data);
if(!pgm_in->data)
{ printf("Memory allocation failed\n"); }
for(row = 0; row <= pgm_in->nr-1; row++)
for(col = 0; col <= pgm_in->nc-1;col++)
{
if(fscanf(fp, "%i", &pgm_in->data[row][col]) == EOF)
{
printf("reached EOF early at: Row:%i\n Col:%i",row,col);
}
}
fclose (fp);
return 0;
}
E:\>lin blam.c
E:\>e:\lint\lint-nt +template(1) +fcp +v -ie:\lint\
std.lnt -os(_lint.tmp) blam.c blam.c0 blam.c1 blam.c2 blam.c3
blam.c4 blam.c5 blam.c6 b
lam.c7 blam.c8 blam.c9 0 1 2
PC-lint for C/C++ (NT) Ver. 7.00j, Copyright Gimpel Software 1985-1997
--- Module: blam.c
E:\>type _lint.tmp | more
--- Module: blam.c
_
pgm_in = malloc(sizeof *pgm_in);
blam.c(37) : Error 64: Type mismatch (assignment) (ptrs to void/nonvoid)
_
pgm_in->magic_str = malloc(10 * sizeof *pgm_in->magic_str);
blam.c(38) : Error 64: Type mismatch (assignment) (ptrs to void/nonvoid)
blam.c(38) : Warning 613: Possible use of null pointer (pgm_in) in left
argument to operator '->'
_
{sscanf(line_in, "%s", &pgm_in->magic_str);}
blam.c(46) : Warning 561: (arg. no. 3) indirect object inconsistent with
format
blam.c(46) : Warning 613: Possible use of null pointer (pgm_in) in left
argument to operator '->'
_
{sscanf(line_in, "%i %i", &pgm_in->nr, &pgm_in->nc);}
blam.c(48) : Warning 613: Possible use of null pointer (pgm_in) in left
argument to operator '->'
blam.c(48) : Warning 613: Possible use of null pointer (pgm_in) in left
argument to operator '->'
_
{sscanf(line_in, "%i", &pgm_in->max_val); break;}
blam.c(50) : Warning 613: Possible use of null pointer (pgm_in) in left
argument to operator '->'
_
printf("PGM Header: Magic # %s\n", &pgm_in->magic_str);
blam.c(54) : Warning 559: Size of argument no. 2 inconsistent with format
blam.c(54) : Warning 613: Possible use of null pointer (pgm_in) in left
argument to operator '->'
_
printf("PGM Header: Rows # %i\n", pgm_in->nr);
blam.c(55) : Warning 613: Possible use of null pointer (pgm_in) in left
argument to operator '->'
_
printf("PGM Header: Columns # %i\n", pgm_in->nc);
blam.c(56) : Warning 613: Possible use of null pointer (pgm_in) in left
argument to operator '->'
_
printf("PGM Header: Max Value # %i\n", pgm_in->max_val);
blam.c(57) : Warning 613: Possible use of null pointer (pgm_in) in left
argument to operator '->'
_
pgm_in->data = malloc( pgm_in->nc * pgm_in->nr * sizeof *pgm_in->data);
blam.c(59) : Info 737: Loss of sign in promotion from int to unsigned int
blam.c(59) : Error 64: Type mismatch (assignment) (ptrs to void/nonvoid)
blam.c(59) : Warning 613: Possible use of null pointer (pgm_in) in left
argument to operator '->'
blam.c(59) : Warning 613: Possible use of null pointer (pgm_in) in left
argument to operator '->'
blam.c(59) : Warning 613: Possible use of null pointer (pgm_in) in left
argument to operator '->'
_
if(!pgm_in->data)
blam.c(60) : Warning 613: Possible use of null pointer (pgm_in) in left
argument to operator '->'
_
for(row = 0; row <= pgm_in->nr-1; row++)
blam.c(63) : Warning 613: Possible use of null pointer (pgm_in) in left
argument to operator '->'
blam.c(63) : Warning 613: Possible use of null pointer (pgm_in) in left
argument to operator '->'
_
for(col = 0; col <= pgm_in->nc-1;col++)
blam.c(64) : Warning 613: Possible use of null pointer (pgm_in) in left
argument to operator '->'
blam.c(64) : Warning 613: Possible use of null pointer (pgm_in) in left
argument to operator '->'
_
if(fscanf(fp, "%i", &pgm_in->data[row][col]) == EOF)
blam.c(66) : Warning 561: (arg. no. 3) indirect object inconsistent with
format
blam.c(66) : Warning 613: Possible use of null pointer (pgm_in) in left
argument to operator '->'
_
fclose (fp);
blam.c(72) : Warning 613: Possible use of null pointer (pgm_in) in left
argument to operator '->'
blam.c(72) : Warning 613: Possible use of null pointer (pgm_in) in left
argument to operator '->'
--- Wrap-up for Module: blam.c
Info 754: local structure member pgmimage:
r (line 9, file blam.c) not
referenced
Info 754: local structure member pgmimage:
y (line 9, file blam.c) not
referenced
Info 766: Header file 'C:\lang\VC98\include\string.h' not used in module
'blam.c'
Error 305: Unable to open module: blam.c0
---
PC-lint for C/C++ output placed in _LINT.TMP
E:\>
E:\>splint blam.c
Splint 3.0.1.6 --- 11 Feb 2002
blam.c: (in function main)
blam.c(38,11): Arrow access from possibly null pointer pgm_in:
pgm_in->magic_str
A possibly null pointer is dereferenced. Value is either the result of a
function which may return null (in which case, code should check it is not
null), or a global, parameter or structure field declared with the null
qualifier. (Use -nullderef to inhibit warning)
blam.c(37,14): Storage pgm_in may become null
blam.c(46,39): Format argument 1 to sscanf (%s) expects char * gets char **:
&pgm_in->magic_str
Type of parameter is not consistent with corresponding code in format
string.
(Use -formattype to inhibit warning)
blam.c(46,35): Corresponding format code
blam.c(46,17): Return value (type int) ignored: sscanf(line_in, ...
Result returned by function call is not used. If this is intended, can
cast
result to (void) to eliminate message. (Use -retvalint to inhibit warning)
blam.c(48,16): Return value (type int) ignored: sscanf(line_in, ...
blam.c(50,16): Return value (type int) ignored: sscanf(line_in, ...
blam.c(54,46): Format argument 1 to printf (%s) expects char * gets char **:
&pgm_in->magic_str
blam.c(54,40): Corresponding format code
blam.c(55,46): Field pgm_in->nr used before definition
An rvalue is used that may not be initialized to a value on some execution
path. (Use -usedef to inhibit warning)
blam.c(56,44): Field pgm_in->nc used before definition
blam.c(57,44): Field pgm_in->max_val used before definition
blam.c(66,34): Index of possibly null pointer pgm_in->data: pgm_in->data
blam.c(59,20): Storage pgm_in->data may become null
blam.c(66,34): Value pgm_in->data[] used before definition
blam.c(66,33): Format argument 1 to fscanf (%i) expects int * gets unsigned
char *: &pgm_in->data[row][col]
To make char and int types equivalent, use +charint.
blam.c(66,29): Corresponding format code
blam.c(66,33): Unallocated storage &pgm_in->data[][] passed as out parameter
to
fscanf: &pgm_in->data[row][col]
blam.c(66,33): Attempt to set unuseable storage: pgm_in->data[][]
blam.c(72,5): Return value (type int) ignored: fclose(fp)
blam.c(73,14): Fresh storage pgm_in not released before return
A memory leak has been detected. Storage allocated locally is not released
before the last reference to it is lost. (Use -mustfreefresh to inhibit
warning)
blam.c(37,5): Fresh storage pgm_in allocated
Finished checking --- 16 code warnings
E:\>