fix false negative introduced by fix for #2641

This commit is contained in:
Robert Reif 2011-03-12 11:42:58 -05:00
parent 0a28b7309f
commit 384bd96766
3 changed files with 18 additions and 3 deletions

View File

@ -412,6 +412,12 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec()
// name of struct pointer
const std::string varname(tok1->str());
// is pointer local?
bool isLocal = false;
const Variable * var = _tokenizer->getSymbolDatabase()->getVariableFromVarId(tok1->varId());
if (var && var->isLocal())
isLocal = true;
// count { and } using tok2
unsigned int indentlevel2 = 0;
for (const Token *tok2 = tok1->tokAt(3); tok2; tok2 = tok2->next())
@ -448,11 +454,10 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec()
else if (indentlevel2 == 0 && tok2->str() == "return")
break;
// Function call: If the pointer is a global variable it
// Function call: If the pointer is not a local 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(), ") ;"))
Token::simpleMatch(tok2->tokAt(2)->link(), ") ;") && !isLocal)
{
break;
}

View File

@ -413,6 +413,7 @@ public:
type == eFor || type == eWhile || type == eDo ||
type == eSwitch || type == eUnconditional);
}
/**
* @brief find if name is in nested list
* @param name name of nested scope

View File

@ -335,6 +335,15 @@ private:
" if (abc) { }\n"
"}");
ASSERT_EQUALS("",errout.str());
// #2641 - local pointer, function call
check("void f() {\n"
" ABC *abc;\n"
" abc->a = 0;\n"
" do_stuff();\n"
" if (abc) { }\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 5\n",errout.str());
}
// Dereferencing a pointer and then checking if it is null