diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 11652d796..3e54b6b58 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -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 diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index c61d7cde1..3f4794212 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -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"