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
}
In such cases, cadvise generates the following warning:
"null.c", line 22, procedure process: warning
Potential null pointer dereference through f is detected (null definition:/home/sandyam/demo/null.c, line 16)
8.2 Detecting generic programming errors 47