Null pointers: Fixed false negative for such code: 'abc->a = 0; if (abc && ..'. Ticket: #2379

This commit is contained in:
Daniel Marjamäki 2011-02-19 21:10:31 +01:00
parent e7ef1b3627
commit 29d05cf5f2
2 changed files with 9 additions and 2 deletions

View File

@ -444,8 +444,8 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec()
break; break;
// Check if pointer is null. // Check if pointer is null.
// TODO: false negatives for something like: "if (p &&.."? // TODO: false negatives for "if (!p || .."
else if (Token::Match(tok2, "if ( !| %varid% )", varid1)) else if (Token::Match(tok2, "if ( !| %varid% )|&&", varid1))
{ {
// Is this variable a pointer? // Is this variable a pointer?
if (isPointer(varid1)) if (isPointer(varid1))

View File

@ -215,6 +215,13 @@ private:
"}\n"); "}\n");
TODO_ASSERT_EQUALS("[test.cpp:2]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 3\n", "", errout.str()); TODO_ASSERT_EQUALS("[test.cpp:2]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 3\n", "", errout.str());
check("void foo(ABC *abc) {\n"
" abc->a = 0;\n"
" if (abc && abc->b == 0)\n"
" ;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 3\n", errout.str());
// ok dereferencing in a condition // ok dereferencing in a condition
check("void foo(struct ABC *abc)\n" check("void foo(struct ABC *abc)\n"
"{\n" "{\n"