Fixed #2166 (false positive: possible null pointer dereference)

This commit is contained in:
Daniel Marjamäki 2010-11-04 18:18:19 +01:00
parent bc8ecf2aa7
commit c29940b114
2 changed files with 17 additions and 5 deletions

View File

@ -402,7 +402,11 @@ void CheckNullPointer::nullPointerByDeRefAndChec()
if (tok1->varId() == varid)
{
bool unknown = false;
if (CheckNullPointer::isPointerDeRef(tok1, unknown))
if (Token::Match(tok1->tokAt(-2), "%varid% = %varid% .", varid))
{
break;
}
else if (CheckNullPointer::isPointerDeRef(tok1, unknown))
{
nullPointerError(tok1, varname, tok->linenr());
break;

View File

@ -36,8 +36,8 @@ private:
{
TEST_CASE(nullpointer1);
TEST_CASE(nullpointer2);
TEST_CASE(nullpointer3); // dereferencing struct and then checking if it's null
TEST_CASE(nullpointer4);
TEST_CASE(structDerefAndCheck); // dereferencing struct and then checking if it's null
TEST_CASE(pointerDerefAndCheck);
TEST_CASE(nullpointer5); // References should not be checked
TEST_CASE(nullpointer6);
TEST_CASE(nullpointer7);
@ -171,7 +171,7 @@ private:
// Dereferencing a struct and then checking if it is null
// This is checked by this function:
// CheckOther::nullPointerStructByDeRefAndChec
void nullpointer3()
void structDerefAndCheck()
{
// errors..
check("void foo(struct ABC *abc)\n"
@ -289,7 +289,7 @@ private:
}
// Dereferencing a pointer and then checking if it is null
void nullpointer4()
void pointerDerefAndCheck()
{
// errors..
check("void foo(int *p)\n"
@ -370,6 +370,14 @@ private:
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void foo(x *p)\n"
"{\n"
" p = p->next;\n"
" if (!p)\n"
" ;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void nullpointer5()