Null pointer: Fixed false positive when condition contains assignment 'if (p==NULL && (p=malloc(10))!=NULL) *p=0;'
This commit is contained in:
parent
cfcfa3f000
commit
a6a966e28e
|
@ -665,6 +665,9 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
|
|||
if (pointerVariables.find(varid) == pointerVariables.end())
|
||||
continue;
|
||||
|
||||
if (Token::Match(vartok->next(), "&& ( %varid% =", varid))
|
||||
continue;
|
||||
|
||||
// if this is true then it is known that the pointer is null
|
||||
bool null = true;
|
||||
|
||||
|
|
|
@ -1203,6 +1203,15 @@ private:
|
|||
" *p = 0;\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 3\n", errout.str());
|
||||
|
||||
// check, assign and use
|
||||
check("void f() {\n"
|
||||
" char *p;\n"
|
||||
" if (p == 0 && (p = malloc(10)) != 0) {\n"
|
||||
" *p = 0;\n"
|
||||
" }\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
// Test CheckNullPointer::nullConstantDereference
|
||||
|
|
Loading…
Reference in New Issue