Null pointers: Fixed false negative when struct is dereferenced in condition

This commit is contained in:
Daniel Marjamäki 2011-08-01 21:57:23 +02:00
parent 9b9a0de777
commit 0c2f2e1c38
2 changed files with 11 additions and 1 deletions

View File

@ -379,9 +379,11 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec()
} }
// dereference in condition // dereference in condition
else if (Token::Match(tok1, "if ( %var% .")) else if (Token::Match(tok1, "if ( !| %var% ."))
{ {
tok1 = tok1->tokAt(2); tok1 = tok1->tokAt(2);
if (tok1->str() == "!")
tok1 = tok1->next();
} }
// dereference in function call (but not sizeof|decltype) // dereference in function call (but not sizeof|decltype)

View File

@ -231,6 +231,14 @@ private:
"}"); "}");
ASSERT_EQUALS("[test.cpp:2]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 5\n", errout.str()); ASSERT_EQUALS("[test.cpp:2]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 5\n", errout.str());
check("void f(ABC *abc) {\n"
" if (abc->x == 0) {\n"
" return;\n"
" }\n"
" if (!abc);\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 5\n", errout.str());
// TODO: False negative if member of member is dereferenced // TODO: False negative if member of member is dereferenced
check("void foo(ABC *abc) {\n" check("void foo(ABC *abc) {\n"
" abc->next->a = 0;\n" " abc->next->a = 0;\n"