diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 91bef73fb..491a9c1dd 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -621,6 +621,8 @@ void CheckNullPointer::nullPointerByCheckAndDeRef() if (varid == 0) continue; + const unsigned int linenr = vartok->linenr(); + // Check if variable is a pointer. TODO: Use isPointer? if (pointerVariables.find(varid) == pointerVariables.end()) continue; @@ -675,7 +677,7 @@ void CheckNullPointer::nullPointerByCheckAndDeRef() if (Token::Match(tok2, "goto|return|continue|break|throw|if|switch")) { if (Token::Match(tok2, "return * %varid%", varid)) - nullPointerError(tok2, tok->strAt(3)); + nullPointerError(tok2, tok->strAt(3), linenr); break; } @@ -705,7 +707,7 @@ void CheckNullPointer::nullPointerByCheckAndDeRef() ; else if (CheckNullPointer::isPointerDeRef(tok2, unknown)) - nullPointerError(tok2, pointerName); + nullPointerError(tok2, pointerName, linenr); else break; diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 4fc97ead9..3b851c9c8 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -855,41 +855,41 @@ private: " }\n" " *p = 0;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: p\n", errout.str()); + ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 2\n", errout.str()); check("void foo(char *p) {\n" " if (NULL == p) {\n" " }\n" " *p = 0;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: p\n", errout.str()); + ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 2\n", errout.str()); check("void foo(char *p) {\n" " if (p == NULL) {\n" " }\n" " *p = 0;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: p\n", errout.str()); + ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 2\n", errout.str()); check("void foo(char *p) {\n" " if (p == NULL) {\n" " }\n" " printf(\"%c\", *p);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: p\n", errout.str()); + ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 2\n", errout.str()); check("void foo(char *p) {\n" " if (p && *p == 0) {\n" " }\n" " printf(\"%c\", *p);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: p\n", errout.str()); + ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 2\n", errout.str()); check("void foo(char *p) {\n" " if (p && *p == 0) {\n" " } else { *p = 0; }\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 2\n", errout.str()); check("void foo(abc *p) {\n" " if (!p) {\n" @@ -971,7 +971,7 @@ private: " }\n" " *p = 0;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:5]: (error) Possible null pointer dereference: p\n", errout.str()); + ASSERT_EQUALS("[test.cpp:5]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 2\n", errout.str()); // #2467 - unknown macro may terminate the application check("void f(Fred *fred) {\n"