I'm getting a warning while doing a Static Analysis (SA) on my code. I have simplified it below (with the first warning)-
typedef struct testStruct_ {
int *ptr;
} testStruct;
testStruct a;
testStruct *a_ptr;
a_ptr = &a;
a_ptr->ptr = NULL; #WARNING: Directly dereferencing pointer a_ptr.
The code goes on to do several operations with a_ptr
.
I've posted an example for the sake of completion-
rc = fn_a (filename, a_ptr);
rc = fn_b (a_ptr);
rc = fn_c (a_ptr->ptr);
fn_a is defined as-
fn_a (const char *filename, testStruct *a_ptr)
{
a_ptr->ptr = fn_a_2(filename);
if (!a_ptr->ptr) {
ERR("Loading (%s) failed", filename);
return (FALSE);
}
return (TRUE);
}
At one point later, I get another warning:
if (a_ptr && a_ptr->ptr) {
freeFn(a_ptr->ptr);
}
#WARNING: Dereference before NULL check - NULL checking a_ptr suggests that it may be NULL, but it has already been dereferenced on all paths leading up to the check.
It seems like the line a_ptr->ptr = NULL
is deemed incorrect/dangerous. Why is this error being shown and is there a way to correct it?