我在对我的代码进行静态分析 (SA) 时收到警告。我在下面简化了它(第一个警告)-
typedef struct testStruct_ {
int *ptr;
} testStruct;
testStruct a;
testStruct *a_ptr;
a_ptr = &a;
a_ptr->ptr = NULL; #WARNING: Directly dereferencing pointer a_ptr.
代码继续使用a_ptr
. 为了完成,我发布了一个示例-
rc = fn_a (filename, a_ptr);
rc = fn_b (a_ptr);
rc = fn_c (a_ptr->ptr);
fn_a 定义为-
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);
}
稍后,我收到另一个警告:
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.
这条线似乎a_ptr->ptr = NULL
被认为是不正确/危险的。为什么会显示这个错误,有没有办法纠正它?