Example 53 Null pointer dereference check

A null pointer may result from null assignment or from a call to the APIs that may return a null value. If a pointer that is not guarded when it is dereferenced, unexpected runtime results may occur. For example, see the following program fragment:

#include <stdio.h>

int process (char* filename)

{

FILE* f;

int count = 0; if (filename)

{

if (f = fopen(filename, "r"))

{

while (fgetc(f) != EOF) count ++;

}

//f can be null at this point fclose (f);

return count;

}

return -1;

}

In such cases, cadvise generates the following warning:

"null.c", line 22, procedure process: warning #20200-D:

Potential null pointer dereference through f is detected (null definition:/home/sandyam/demo/null.c, line 16)

8.2 Detecting generic programming errors 47

Page 47
Image 47
HP UX Web Development Tools Example 53 Null pointer dereference check, Such cases, cadvise generates the following warning