fix false negative introduced by fix for #2641
This commit is contained in:
parent
0a28b7309f
commit
384bd96766
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue