diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 24e28915f..c8224b3bc 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -863,7 +863,24 @@ void CheckNullPointer::nullPointerByDeRefAndChec() // uncertain bool unknown = _settings->inconclusive; - if (Token::Match(tok1->tokAt(-2), "%varid% = %varid% .", varid)) { + // reassign : is the pointer reassigned like this: + // tok = tok->next(); + bool reassign = false; + if (Token::Match(tok1->previous(), "= %varid% .", varid)) { + const Token *back = tok1->tokAt(-2); + while (back) { + if (back->varId() == varid) { + reassign = true; + break; + } + if (Token::Match(back, "[{};,(]")) { + break; + } + back = back->previous(); + } + } + + if (reassign) { break; } else if (Token::simpleMatch(tok1->tokAt(-2), "* )") && Token::Match(tok1->linkAt(-1)->tokAt(-2), "%varid% = (", tok1->varId())) { diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 520a946af..41846aef8 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -653,6 +653,14 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + check("void foo(x *p)\n" + "{\n" + " p = *p2 = p->next;\n" + " if (!p)\n" + " ;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + check("void foo(struct ABC *abc)\n" "{\n" " abc = abc ? abc->next : 0;\n"