Null pointer: prefer longer error message that says 'otherwise condition at line X is redundant'

This commit is contained in:
Daniel Marjamäki 2011-03-28 18:48:27 +02:00
parent 7426bd3daf
commit a1dba61cee
2 changed files with 11 additions and 9 deletions

View File

@ -621,6 +621,8 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
if (varid == 0) if (varid == 0)
continue; continue;
const unsigned int linenr = vartok->linenr();
// Check if variable is a pointer. TODO: Use isPointer? // Check if variable is a pointer. TODO: Use isPointer?
if (pointerVariables.find(varid) == pointerVariables.end()) if (pointerVariables.find(varid) == pointerVariables.end())
continue; continue;
@ -675,7 +677,7 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
if (Token::Match(tok2, "goto|return|continue|break|throw|if|switch")) if (Token::Match(tok2, "goto|return|continue|break|throw|if|switch"))
{ {
if (Token::Match(tok2, "return * %varid%", varid)) if (Token::Match(tok2, "return * %varid%", varid))
nullPointerError(tok2, tok->strAt(3)); nullPointerError(tok2, tok->strAt(3), linenr);
break; break;
} }
@ -705,7 +707,7 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
; ;
else if (CheckNullPointer::isPointerDeRef(tok2, unknown)) else if (CheckNullPointer::isPointerDeRef(tok2, unknown))
nullPointerError(tok2, pointerName); nullPointerError(tok2, pointerName, linenr);
else else
break; break;

View File

@ -855,41 +855,41 @@ private:
" }\n" " }\n"
" *p = 0;\n" " *p = 0;\n"
"}\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" check("void foo(char *p) {\n"
" if (NULL == p) {\n" " if (NULL == p) {\n"
" }\n" " }\n"
" *p = 0;\n" " *p = 0;\n"
"}\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" check("void foo(char *p) {\n"
" if (p == NULL) {\n" " if (p == NULL) {\n"
" }\n" " }\n"
" *p = 0;\n" " *p = 0;\n"
"}\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" check("void foo(char *p) {\n"
" if (p == NULL) {\n" " if (p == NULL) {\n"
" }\n" " }\n"
" printf(\"%c\", *p);\n" " printf(\"%c\", *p);\n"
"}\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" check("void foo(char *p) {\n"
" if (p && *p == 0) {\n" " if (p && *p == 0) {\n"
" }\n" " }\n"
" printf(\"%c\", *p);\n" " printf(\"%c\", *p);\n"
"}\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" check("void foo(char *p) {\n"
" if (p && *p == 0) {\n" " if (p && *p == 0) {\n"
" } else { *p = 0; }\n" " } else { *p = 0; }\n"
"}\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" check("void foo(abc *p) {\n"
" if (!p) {\n" " if (!p) {\n"
@ -971,7 +971,7 @@ private:
" }\n" " }\n"
" *p = 0;\n" " *p = 0;\n"
"}\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 // #2467 - unknown macro may terminate the application
check("void f(Fred *fred) {\n" check("void f(Fred *fred) {\n"