diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 9a0687e1f..cb8ed8898 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -402,7 +402,11 @@ void CheckNullPointer::nullPointerByDeRefAndChec() if (tok1->varId() == varid) { bool unknown = false; - if (CheckNullPointer::isPointerDeRef(tok1, unknown)) + if (Token::Match(tok1->tokAt(-2), "%varid% = %varid% .", varid)) + { + break; + } + else if (CheckNullPointer::isPointerDeRef(tok1, unknown)) { nullPointerError(tok1, varname, tok->linenr()); break; diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index b1b3295eb..35e732fda 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -36,8 +36,8 @@ private: { TEST_CASE(nullpointer1); TEST_CASE(nullpointer2); - TEST_CASE(nullpointer3); // dereferencing struct and then checking if it's null - TEST_CASE(nullpointer4); + TEST_CASE(structDerefAndCheck); // dereferencing struct and then checking if it's null + TEST_CASE(pointerDerefAndCheck); TEST_CASE(nullpointer5); // References should not be checked TEST_CASE(nullpointer6); TEST_CASE(nullpointer7); @@ -171,7 +171,7 @@ private: // Dereferencing a struct and then checking if it is null // This is checked by this function: // CheckOther::nullPointerStructByDeRefAndChec - void nullpointer3() + void structDerefAndCheck() { // errors.. check("void foo(struct ABC *abc)\n" @@ -289,7 +289,7 @@ private: } // Dereferencing a pointer and then checking if it is null - void nullpointer4() + void pointerDerefAndCheck() { // errors.. check("void foo(int *p)\n" @@ -370,6 +370,14 @@ private: " }\n" "}"); ASSERT_EQUALS("", errout.str()); + + check("void foo(x *p)\n" + "{\n" + " p = p->next;\n" + " if (!p)\n" + " ;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void nullpointer5()