Null pointers: fixed false negatives when there are '(p && ..'

This commit is contained in:
Daniel Marjamäki 2011-10-08 12:58:10 +02:00
parent 7d13d25638
commit 0b09732881
2 changed files with 11 additions and 4 deletions

View File

@ -576,12 +576,12 @@ void CheckNullPointer::nullPointerByDeRefAndChec()
if (Token::Match(tok1->link()->previous(), "while ( %varid%", varid))
break;
// TODO: there might be false negatives. perhaps
// instead of bailing out it's ok to skip the condition.
// this bailout is related to #3128
if (Token::Match(tok1->link(), "( ! %varid% ||", varid) ||
Token::Match(tok1->link(), "( %varid% &&", varid))
break;
{
tok1 = tok1->link();
continue;
}
if (Token::simpleMatch(tok1->link()->previous(), "sizeof ("))
{

View File

@ -544,6 +544,13 @@ private:
"}");
ASSERT_EQUALS("", errout.str());
check("void f(int *p) {\n"
" *p = 12;\n"
" assert(p && (*p<=6));\n"
" if (p) { *p = 0; }\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 4\n", errout.str());
check("void foo(x *p)\n"
"{\n"
" p = p->next;\n"