CheckNullPointer: Fix FP for 'buf[p]' when p is NULL

This commit is contained in:
Daniel Marjamäki 2016-01-20 08:42:58 +01:00
parent ab441f3fb1
commit 4a3ece5847
2 changed files with 7 additions and 2 deletions

View File

@ -160,7 +160,7 @@ bool CheckNullPointer::isPointerDeRef(const Token *tok, bool &unknown)
const Token* parent = tok->astParent();
if (!parent)
return false;
bool firstOperand = parent->astOperand1() == tok;
const bool firstOperand = parent->astOperand1() == tok;
while (parent->str() == "(" && (parent->astOperand2() == nullptr && parent->strAt(1) != ")")) { // Skip over casts
parent = parent->astParent();
if (!parent)
@ -172,7 +172,7 @@ bool CheckNullPointer::isPointerDeRef(const Token *tok, bool &unknown)
return true;
// array access
if (parent->str() == "[" && (!parent->astParent() || parent->astParent()->str() != "&"))
if (firstOperand && parent->str() == "[" && (!parent->astParent() || parent->astParent()->str() != "&"))
return true;
// address of member variable / array element

View File

@ -2353,6 +2353,11 @@ private:
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Possible null pointer dereference if the default parameter value is used: p\n", errout.str());
check("void f(int *p = 0) {\n"
" buf[p] = 0;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(int *p = 0) {\n"
" if (p != 0 && bar())\n"
" *p = 0;\n"