diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index df4b6845c..430ae626a 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -1223,7 +1223,12 @@ void CheckNullPointer::nullPointerDefaultArgument() } } if (dependsOnPointer && Token::simpleMatch(endOfCondition, ") {")) { - tok = endOfCondition->next()->link(); + const Token *endOfIf = endOfCondition->next()->link(); + for (; tok != endOfIf; tok = tok->next()) { + // If a pointer is assigned a new value, stop considering it. + if (Token::Match(tok, "%var% =")) + pointerArgs.erase(tok->varId()); + } continue; } } @@ -1232,7 +1237,7 @@ void CheckNullPointer::nullPointerDefaultArgument() continue; // If a pointer is assigned a new value, stop considering it. - if (Token::Match(tok, "%var% = %any%")) + if (Token::Match(tok, "%var% =")) pointerArgs.erase(tok->varId()); if (isPointerDeRef(tok, unknown, symbolDatabase)) diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 0e4d4a563..790c32bac 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -2193,6 +2193,14 @@ private: "}"); ASSERT_EQUALS("", errout.str()); + check("void f(int *p = 0) {\n" + " int y;\n" + " if (p == 0)\n" + " p = &y;\n" + " *p = 0;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + check("void f(int *p = 0) {\n" " if (a != 0)\n" " *p = 0;\n"