diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index bc8324e79..242426a62 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -629,6 +629,9 @@ void CheckNullPointer::nullPointerByCheckAndDeRef() // start token = inside the if-body const Token *tok1 = tok->next()->link()->tokAt(2); + // indentlevel inside the if-body is 1 + unsigned int indentlevel = 1; + if (Token::Match(tok, "if|while ( %var% )|&&")) { // pointer might be null @@ -639,13 +642,15 @@ void CheckNullPointer::nullPointerByCheckAndDeRef() tok1 = tok1 ? tok1->next() : NULL; if (!tok1) continue; + + // indentlevel at the base level is 0 + indentlevel = 0; } // Name of the pointer const std::string &pointerName = vartok->str(); // Count { and } for tok2 - unsigned int indentlevel = 1; for (const Token *tok2 = tok1; tok2; tok2 = tok2->next()) { if (tok2->str() == "{") @@ -655,13 +660,6 @@ void CheckNullPointer::nullPointerByCheckAndDeRef() if (indentlevel == 0) break; --indentlevel; - if (null && indentlevel == 0) - { - // skip all "else" blocks because they are not executed in this execution path - while (Token::simpleMatch(tok2, "} else {")) - tok2 = tok2->tokAt(2)->link(); - null = false; - } } if (Token::Match(tok2, "goto|return|continue|break|throw|if|switch")) diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 811726fcb..421aa137b 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -984,6 +984,23 @@ private: // #2582 - segmentation fault check("if()"); + + // #2674 - different functions + check("class Fred {\n" + "public:\n" + " Wilma *wilma;\n" + " void a();\n" + " void b();\n" + "};\n" + "\n" + "void Fred::a() {\n" + " if ( wilma ) { }\n" + "}\n" + "\n" + "void Fred::b() {\n" + " wilma->Reload();\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } // Test CheckNullPointer::nullConstantDereference