Fixed #2641 (False positive: Possible null pointer dereference (global pointer, function call))

This commit is contained in:
Daniel Marjamäki 2011-03-12 15:02:06 +01:00
parent 29ab409af5
commit 0a28b7309f
2 changed files with 18 additions and 0 deletions

View File

@ -448,6 +448,15 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec()
else if (indentlevel2 == 0 && tok2->str() == "return") else if (indentlevel2 == 0 && tok2->str() == "return")
break; break;
// Function call: If the pointer is a global variable it
// might be changed by the call.
// TODO: false negatives if the pointer is local.
else if (Token::Match(tok2, "[;{}] %var% (") &&
Token::simpleMatch(tok2->tokAt(2)->link(), ") ;"))
{
break;
}
// Check if pointer is null. // Check if pointer is null.
// TODO: false negatives for "if (!p || .." // TODO: false negatives for "if (!p || .."
else if (Token::Match(tok2, "if ( !| %varid% )|&&", varid1)) else if (Token::Match(tok2, "if ( !| %varid% )|&&", varid1))

View File

@ -326,6 +326,15 @@ private:
" ;\n" " ;\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// #2641 - global pointer, function call
check("ABC *abc;\n"
"void f() {\n"
" abc->a = 0;\n"
" do_stuff();\n"
" if (abc) { }\n"
"}");
ASSERT_EQUALS("",errout.str());
} }
// Dereferencing a pointer and then checking if it is null // Dereferencing a pointer and then checking if it is null