Message refactorization: checknullpointer.cpp

Removed a duplicate test in checknullpointer.cpp
This commit is contained in:
PKEuS 2012-08-05 01:38:48 -07:00
parent 5e862351e9
commit 25ecd3ed71
3 changed files with 71 additions and 74 deletions

View File

@ -472,11 +472,11 @@ void CheckNullPointer::nullPointerAfterLoop()
// Is the loop variable dereferenced? // Is the loop variable dereferenced?
if (CheckNullPointer::isPointerDeRef(tok2, unknown, symbolDatabase)) { if (CheckNullPointer::isPointerDeRef(tok2, unknown, symbolDatabase)) {
nullPointerError(tok2, varname, tok->linenr(), inconclusive); nullPointerError(tok2, varname, tok, inconclusive);
} }
else if (unknown && _settings->inconclusive) { else if (unknown && _settings->inconclusive) {
nullPointerError(tok2, varname, tok->linenr(), true); nullPointerError(tok2, varname, tok, true);
} }
break; break;
@ -537,7 +537,7 @@ void CheckNullPointer::nullPointerLinkedList()
++indentlevel4; ++indentlevel4;
else if (tok4->str() == "}") { else if (tok4->str() == "}") {
if (indentlevel4 <= 1) { if (indentlevel4 <= 1) {
nullPointerError(tok1, var->name(), scope->classDef->linenr()); nullPointerError(tok1, var->name(), scope->classDef);
break; break;
} }
--indentlevel4; --indentlevel4;
@ -740,7 +740,7 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec()
else if (!tok2->isExpandedMacro() && Token::Match(tok2, "if ( !| %varid% )|&&", varid1)) { else if (!tok2->isExpandedMacro() && Token::Match(tok2, "if ( !| %varid% )|&&", varid1)) {
// Is this variable a pointer? // Is this variable a pointer?
if (var->isPointer()) if (var->isPointer())
nullPointerError(tok1, varname, tok2->linenr(), inconclusive); nullPointerError(tok1, varname, tok2, inconclusive);
break; break;
} }
} }
@ -808,7 +808,7 @@ void CheckNullPointer::nullPointerByDeRefAndChec()
std::list<const Token *> varlist; std::list<const Token *> varlist;
parseFunctionCall(*(tok2->next()), varlist, 0); parseFunctionCall(*(tok2->next()), varlist, 0);
if (!varlist.empty() && varlist.front() == tok2->tokAt(3)) { if (!varlist.empty() && varlist.front() == tok2->tokAt(3)) {
nullPointerError(tok2->tokAt(3), varname, tok->linenr(), inconclusive); nullPointerError(tok2->tokAt(3), varname, tok, inconclusive);
break; break;
} }
} }
@ -886,7 +886,7 @@ void CheckNullPointer::nullPointerByDeRefAndChec()
} else if (Token::Match(tok1->tokAt(-2), "&&|%oror% !")) { } else if (Token::Match(tok1->tokAt(-2), "&&|%oror% !")) {
break; break;
} else if (CheckNullPointer::isPointerDeRef(tok1, unknown, symbolDatabase)) { } else if (CheckNullPointer::isPointerDeRef(tok1, unknown, symbolDatabase)) {
nullPointerError(tok1, varname, tok->linenr(), inconclusive); nullPointerError(tok1, varname, tok, inconclusive);
break; break;
} else if (tok1->strAt(-1) == "&") { } else if (tok1->strAt(-1) == "&") {
break; break;
@ -958,7 +958,6 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
// Name and line of the pointer // Name and line of the pointer
const std::string &pointerName = vartok->str(); const std::string &pointerName = vartok->str();
const unsigned int linenr = vartok->linenr();
// if this is true then it is known that the pointer is null // if this is true then it is known that the pointer is null
bool null = true; bool null = true;
@ -973,12 +972,12 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
} }
// Pointer is used // Pointer is used
else if (Token::Match(tok2, "* %varid%", varid)) { else if (Token::Match(tok2, "* %varid%", varid)) {
nullPointerError(tok2->tokAt(2), pointerName, linenr, false); nullPointerError(tok2->tokAt(2), pointerName, vartok, false);
break; break;
} }
// Pointer is used // Pointer is used
else if (Token::Match(tok2, "%varid% .", varid)) { else if (Token::Match(tok2, "%varid% .", varid)) {
nullPointerError(tok2, pointerName, linenr, false); nullPointerError(tok2, pointerName, vartok, false);
break; break;
} }
} }
@ -1033,9 +1032,9 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
for (; tok2 && tok2->str() != ";"; tok2 = tok2->next()) { for (; tok2 && tok2->str() != ";"; tok2 = tok2->next()) {
if (tok2->varId() == varid) { if (tok2->varId() == varid) {
if (CheckNullPointer::isPointerDeRef(tok2, unknown, symbolDatabase)) if (CheckNullPointer::isPointerDeRef(tok2, unknown, symbolDatabase))
nullPointerError(tok2, pointerName, linenr, inconclusive); nullPointerError(tok2, pointerName, vartok, inconclusive);
else if (unknown) else if (unknown)
nullPointerError(tok2, pointerName, linenr, true); nullPointerError(tok2, pointerName, vartok, true);
} }
} }
break; break;
@ -1059,7 +1058,7 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
parseFunctionCall(*tok2, vars, 0); parseFunctionCall(*tok2, vars, 0);
for (std::list<const Token *>::const_iterator it = vars.begin(); it != vars.end(); ++it) { for (std::list<const Token *>::const_iterator it = vars.begin(); it != vars.end(); ++it) {
if (Token::Match(*it, "%varid% [,)]", varid)) { if (Token::Match(*it, "%varid% [,)]", varid)) {
nullPointerError(*it, pointerName, linenr, inconclusive); nullPointerError(*it, pointerName, vartok, inconclusive);
break; break;
} }
} }
@ -1093,10 +1092,10 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
; ;
else if (CheckNullPointer::isPointerDeRef(tok2, unknown, symbolDatabase)) else if (CheckNullPointer::isPointerDeRef(tok2, unknown, symbolDatabase))
nullPointerError(tok2, pointerName, linenr, inconclusive); nullPointerError(tok2, pointerName, vartok, inconclusive);
else if (unknown && _settings->inconclusive) else if (unknown && _settings->inconclusive)
nullPointerError(tok2, pointerName, linenr, true); nullPointerError(tok2, pointerName, vartok, true);
else else
break; break;
@ -1411,8 +1410,11 @@ void CheckNullPointer::nullPointerError(const Token *tok, const std::string &var
reportError(tok, Severity::error, "nullPointer", "Possible null pointer dereference: " + varname); reportError(tok, Severity::error, "nullPointer", "Possible null pointer dereference: " + varname);
} }
void CheckNullPointer::nullPointerError(const Token *tok, const std::string &varname, const unsigned int line, bool inconclusive) void CheckNullPointer::nullPointerError(const Token *tok, const std::string &varname, const Token* nullCheck, bool inconclusive)
{ {
const std::string errmsg("Possible null pointer dereference: " + varname + " - otherwise it is redundant to check if " + varname + " is null at line " + MathLib::toString<unsigned int>(line)); std::list<const Token*> callstack;
reportError(tok, Severity::error, "nullPointer", errmsg, inconclusive); callstack.push_back(tok);
callstack.push_back(nullCheck);
const std::string errmsg("Possible null pointer dereference: " + varname + " - otherwise it is redundant to check it against null.");
reportError(callstack, Severity::error, "nullPointer", errmsg, inconclusive);
} }

View File

@ -99,7 +99,7 @@ public:
void nullPointerError(const Token *tok); // variable name unknown / doesn't exist void nullPointerError(const Token *tok); // variable name unknown / doesn't exist
void nullPointerError(const Token *tok, const std::string &varname); void nullPointerError(const Token *tok, const std::string &varname);
void nullPointerError(const Token *tok, const std::string &varname, const unsigned int line, bool inconclusive = false); void nullPointerError(const Token *tok, const std::string &varname, const Token* nullcheck, bool inconclusive = false);
private: private:

View File

@ -103,7 +103,7 @@ private:
" while (tok);\n" " while (tok);\n"
" tok = tok->next();\n" " tok = tok->next();\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: tok - otherwise it is redundant to check if tok is null at line 3\n", errout.str()); ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:3]: (error) Possible null pointer dereference: tok - otherwise it is redundant to check it against null.\n", errout.str());
// #2681 // #2681
{ {
@ -120,7 +120,7 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check(code, true); // inconclusive=true => error check(code, true); // inconclusive=true => error
ASSERT_EQUALS("[test.cpp:6]: (error, inconclusive) Possible null pointer dereference: tok - otherwise it is redundant to check if tok is null at line 3\n", errout.str()); ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:3]: (error, inconclusive) Possible null pointer dereference: tok - otherwise it is redundant to check it against null.\n", errout.str());
} }
check("void foo()\n" check("void foo()\n"
@ -131,7 +131,7 @@ private:
" tok = tok->next();\n" " tok = tok->next();\n"
" }\n" " }\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: tok - otherwise it is redundant to check if tok is null at line 5\n", errout.str()); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (error) Possible null pointer dereference: tok - otherwise it is redundant to check it against null.\n", errout.str());
check("void foo(Token &tok)\n" check("void foo(Token &tok)\n"
"{\n" "{\n"
@ -192,7 +192,7 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check(code, true); check(code, true);
ASSERT_EQUALS("[test.cpp:5]: (error, inconclusive) Possible null pointer dereference: tok - otherwise it is redundant to check if tok is null at line 3\n", errout.str()); ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (error, inconclusive) Possible null pointer dereference: tok - otherwise it is redundant to check it against null.\n", errout.str());
} }
check("int foo(const Token *tok)\n" check("int foo(const Token *tok)\n"
@ -264,14 +264,14 @@ private:
" if (!abc)\n" " if (!abc)\n"
" ;\n" " ;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 4\n", errout.str()); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check it against null.\n", errout.str());
check("void foo(struct ABC *abc) {\n" check("void foo(struct ABC *abc) {\n"
" bar(abc->a);\n" " bar(abc->a);\n"
" if (!abc)\n" " if (!abc)\n"
" ;\n" " ;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:2]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 3\n", errout.str()); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check it against null.\n", errout.str());
check("void foo(ABC *abc) {\n" check("void foo(ABC *abc) {\n"
" if (abc->a == 3) {\n" " if (abc->a == 3) {\n"
@ -279,7 +279,7 @@ private:
" }\n" " }\n"
" if (abc) {}\n" " if (abc) {}\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:2]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 5\n", errout.str()); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:5]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check it against null.\n", errout.str());
check("void f(ABC *abc) {\n" check("void f(ABC *abc) {\n"
" if (abc->x == 0) {\n" " if (abc->x == 0) {\n"
@ -287,7 +287,7 @@ private:
" }\n" " }\n"
" if (!abc);\n" " if (!abc);\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:2]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 5\n", errout.str()); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:5]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check it against null.\n", errout.str());
// TODO: False negative if member of member is dereferenced // TODO: False negative if member of member is dereferenced
check("void foo(ABC *abc) {\n" check("void foo(ABC *abc) {\n"
@ -295,14 +295,14 @@ private:
" if (abc->next)\n" " if (abc->next)\n"
" ;\n" " ;\n"
"}\n"); "}\n");
TODO_ASSERT_EQUALS("[test.cpp:2]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 3\n", "", errout.str()); TODO_ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check it against null.\n", "", errout.str());
check("void foo(ABC *abc) {\n" check("void foo(ABC *abc) {\n"
" abc->a = 0;\n" " abc->a = 0;\n"
" if (abc && abc->b == 0)\n" " if (abc && abc->b == 0)\n"
" ;\n" " ;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:2]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 3\n", errout.str()); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check it against null.\n", errout.str());
// ok dereferencing in a condition // ok dereferencing in a condition
check("void foo(struct ABC *abc)\n" check("void foo(struct ABC *abc)\n"
@ -425,7 +425,7 @@ private:
" do_stuff();\n" " do_stuff();\n"
" if (abc) { }\n" " if (abc) { }\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 5\n",errout.str()); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check it against null.\n",errout.str());
// #2641 - local pointer, function call // #2641 - local pointer, function call
check("void f(ABC *abc) {\n" check("void f(ABC *abc) {\n"
@ -433,7 +433,7 @@ private:
" do_stuff();\n" " do_stuff();\n"
" if (abc) { }\n" " if (abc) { }\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:2]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 4\n",errout.str()); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check it against null.\n",errout.str());
// #2691 - switch/break // #2691 - switch/break
check("void f(ABC *abc) {\n" check("void f(ABC *abc) {\n"
@ -464,7 +464,7 @@ private:
check(code); check(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check(code, true); check(code, true);
ASSERT_EQUALS("[test.cpp:2]: (error, inconclusive) Possible null pointer dereference: fred - otherwise it is redundant to check if fred is null at line 3\n", errout.str()); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (error, inconclusive) Possible null pointer dereference: fred - otherwise it is redundant to check it against null.\n", errout.str());
} }
// false positives when there are macros // false positives when there are macros
@ -484,21 +484,21 @@ private:
" if (!p)\n" " if (!p)\n"
" ;\n" " ;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 4\n", errout.str()); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
check("void foo(int *p)\n" check("void foo(int *p)\n"
"{\n" "{\n"
" *p = 0;\n" " *p = 0;\n"
" if (p) { }\n" " if (p) { }\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 4\n", errout.str()); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
check("void foo(int *p)\n" check("void foo(int *p)\n"
"{\n" "{\n"
" *p = 0;\n" " *p = 0;\n"
" if (p || q) { }\n" " if (p || q) { }\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 4\n", errout.str()); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
check("void foo(int *p)\n" check("void foo(int *p)\n"
"{\n" "{\n"
@ -506,7 +506,7 @@ private:
" if (!p)\n" " if (!p)\n"
" ;\n" " ;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 4\n", errout.str()); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
check("void foo(char *p)\n" check("void foo(char *p)\n"
"{\n" "{\n"
@ -514,14 +514,14 @@ private:
" if (!p)\n" " if (!p)\n"
" ;\n" " ;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 4\n", errout.str()); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
check("void foo(char *p)\n" check("void foo(char *p)\n"
"{\n" "{\n"
" if (*p == 0) { }\n" " if (*p == 0) { }\n"
" if (!p) { }\n" " if (!p) { }\n"
"}\n"); "}\n");
TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 4\n", "", errout.str()); TODO_ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", "", errout.str());
// no error // no error
check("void foo()\n" check("void foo()\n"
@ -629,7 +629,7 @@ private:
" assert(p && (*p<=6));\n" " assert(p && (*p<=6));\n"
" if (p) { *p = 0; }\n" " if (p) { *p = 0; }\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:2]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 4\n", errout.str()); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
check("void foo(x *p)\n" check("void foo(x *p)\n"
"{\n" "{\n"
@ -682,7 +682,7 @@ private:
" a = b ? c : d;\n" " a = b ? c : d;\n"
" if (item) { }\n" " if (item) { }\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:2]: (error) Possible null pointer dereference: item - otherwise it is redundant to check if item is null at line 4\n", errout.str()); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (error) Possible null pointer dereference: item - otherwise it is redundant to check it against null.\n", errout.str());
check("BOOL GotoFlyAnchor()\n" // #2243 check("BOOL GotoFlyAnchor()\n" // #2243
"{\n" "{\n"
@ -1297,48 +1297,48 @@ private:
" }\n" " }\n"
" *p = 0;\n" " *p = 0;\n"
"}\n"); "}\n");
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()); ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:2]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\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 - otherwise it is redundant to check if p is null at line 2\n", errout.str()); ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:2]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\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 - otherwise it is redundant to check if p is null at line 2\n", errout.str()); ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:2]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\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 - otherwise it is redundant to check if p is null at line 2\n", errout.str()); ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:2]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\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 - otherwise it is redundant to check if p is null at line 2\n", errout.str()); ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:2]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\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 - otherwise it is redundant to check if p is null at line 2\n", errout.str()); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
check("void foo(char *p) {\n" check("void foo(char *p) {\n"
" if (p) {\n" " if (p) {\n"
" }\n" " }\n"
" strcpy(p, \"abc\");\n" " strcpy(p, \"abc\");\n"
"}\n"); "}\n");
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()); ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:2]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
check("void foo(char *p) {\n" check("void foo(char *p) {\n"
" if (p) {\n" " if (p) {\n"
@ -1346,7 +1346,7 @@ private:
" bar();\n" " bar();\n"
" strcpy(p, \"abc\");\n" " strcpy(p, \"abc\");\n"
"}\n"); "}\n");
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()); ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:2]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
check("void foo(abc *p) {\n" check("void foo(abc *p) {\n"
" if (!p) {\n" " if (!p) {\n"
@ -1370,7 +1370,7 @@ private:
" }\n" " }\n"
" *p = 0;\n" " *p = 0;\n"
"}\n", true); "}\n", true);
ASSERT_EQUALS("[test.cpp:5]: (error, inconclusive) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 2\n", errout.str()); ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:2]: (error, inconclusive) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
check("void foo(char *p) {\n" check("void foo(char *p) {\n"
" if (!p) {\n" " if (!p) {\n"
@ -1416,8 +1416,8 @@ private:
" return 5+*p;\n" " return 5+*p;\n"
" }\n" " }\n"
"}"); "}");
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" ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n"
"[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()); "[test.cpp:4] -> [test.cpp:2]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
// operator! // operator!
check("void f() {\n" check("void f() {\n"
@ -1453,7 +1453,7 @@ private:
" }\n" " }\n"
" *p = 0;\n" " *p = 0;\n"
"}\n"); "}\n");
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()); ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:2]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\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"
@ -1549,7 +1549,7 @@ private:
" if (fred) { int a = 0; }\n" " if (fred) { int a = 0; }\n"
" return fred->a;\n" " return fred->a;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: fred - otherwise it is redundant to check if fred is null at line 2\n", errout.str()); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (error) Possible null pointer dereference: fred - otherwise it is redundant to check it against null.\n", errout.str());
// #2789 - assign and check pointer // #2789 - assign and check pointer
check("void f() {\n" check("void f() {\n"
@ -1557,7 +1557,7 @@ private:
" if (!(p=x())) { }\n" " if (!(p=x())) { }\n"
" *p = 0;\n" " *p = 0;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 3\n", errout.str()); ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:3]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
// check, assign and use // check, assign and use
check("void f() {\n" check("void f() {\n"
@ -1584,7 +1584,7 @@ private:
" return;\n" " return;\n"
" }\n" " }\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 3\n", errout.str()); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:3]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
// check, and use // check, and use
check("void f() {\n" check("void f() {\n"
@ -1593,7 +1593,7 @@ private:
" return;\n" " return;\n"
" }\n" " }\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 3\n", errout.str()); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:3]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
// check, and use // check, and use
check("void f() {\n" check("void f() {\n"
@ -1611,7 +1611,7 @@ private:
" return;\n" " return;\n"
" }\n" " }\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 3\n", errout.str()); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:3]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
// check, and use // check, and use
check("void f(struct X *p, int x) {\n" check("void f(struct X *p, int x) {\n"
@ -1631,7 +1631,7 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check(code, true); // inconclusive check(code, true); // inconclusive
ASSERT_EQUALS("[test.cpp:3]: (error, inconclusive) Possible null pointer dereference: fred - otherwise it is redundant to check if fred is null at line 2\n", errout.str()); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (error, inconclusive) Possible null pointer dereference: fred - otherwise it is redundant to check it against null.\n", errout.str());
} }
check("void f(char *s) {\n" // #3358 check("void f(char *s) {\n" // #3358
@ -1801,11 +1801,6 @@ private:
"}"); "}");
ASSERT_EQUALS("[test.cpp:2]: (error) Null pointer dereference\n", errout.str()); ASSERT_EQUALS("[test.cpp:2]: (error) Null pointer dereference\n", errout.str());
check("void f(char* s) {\n"
" printf(\"%s\", s);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(char* foo) {\n" check("void f(char* foo) {\n"
" char location[200];\n" " char location[200];\n"
" int width, height;\n" " int width, height;\n"
@ -1990,8 +1985,8 @@ private:
"}"); "}");
TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p\n" TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p\n"
"[test.cpp:4]: (error) Possible null pointer dereference: p\n" "[test.cpp:4]: (error) Possible null pointer dereference: p\n"
"[test.cpp:6]: (error) Possible null pointer dereference: q - otherwise it is redundant to check if q is null at line 5\n", "[test.cpp:6] -> [test.cpp:5]: (error) Possible null pointer dereference: q - otherwise it is redundant to check it against null.\n",
"[test.cpp:6]: (error) Possible null pointer dereference: q - otherwise it is redundant to check if q is null at line 5\n", errout.str()); "[test.cpp:6] -> [test.cpp:5]: (error) Possible null pointer dereference: q - otherwise it is redundant to check it against null.\n", errout.str());
check("void f(const char* p) {\n" check("void f(const char* p) {\n"
" if(p == 0) {\n" " if(p == 0) {\n"
@ -2001,10 +1996,10 @@ private:
" std::cout << abc << p;\n" " std::cout << abc << p;\n"
" }\n" " }\n"
"}"); "}");
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" ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n"
"[test.cpp:4]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 2\n" "[test.cpp:4] -> [test.cpp:2]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n"
"[test.cpp:5]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 2\n" "[test.cpp:5] -> [test.cpp:2]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n"
"[test.cpp:6]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 2\n", errout.str()); "[test.cpp:6] -> [test.cpp:2]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
check("void f() {\n" check("void f() {\n"
" void* p1 = 0;\n" " void* p1 = 0;\n"
@ -2043,7 +2038,7 @@ private:
" foo(p);\n" " foo(p);\n"
" if (p) { }\n" " if (p) { }\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 6\n", errout.str()); ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:6]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
// function implementation not seen // function implementation not seen
check("void foo(int *p);\n" check("void foo(int *p);\n"
@ -2053,7 +2048,7 @@ private:
" foo(p);\n" " foo(p);\n"
" if (p) { }\n" " if (p) { }\n"
"}"); "}");
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 6\n", "", errout.str()); TODO_ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:6]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", "", errout.str());
// inconclusive // inconclusive
check("void f(int *p) {\n" check("void f(int *p) {\n"
@ -2061,7 +2056,7 @@ private:
" foo(p);\n" " foo(p);\n"
" if (p) { }\n" " if (p) { }\n"
"}", true); "}", true);
ASSERT_EQUALS("[test.cpp:2]: (error, inconclusive) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 4\n", errout.str()); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (error, inconclusive) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
} }
// dereference struct pointer and then check if it's null // dereference struct pointer and then check if it's null
@ -2082,7 +2077,7 @@ private:
" foo(abc);\n" " foo(abc);\n"
" if (abc) { }\n" " if (abc) { }\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 6\n", errout.str()); ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:6]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check it against null.\n", errout.str());
// function implementation not seen // function implementation not seen
check("void foo(struct ABC *abc);\n" check("void foo(struct ABC *abc);\n"
@ -2092,7 +2087,7 @@ private:
" foo(abc);\n" " foo(abc);\n"
" if (abc) { }\n" " if (abc) { }\n"
"}"); "}");
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 6\n", "", errout.str()); TODO_ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:6]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check it against null.\n", "", errout.str());
// inconclusive // inconclusive
check("void f(struct ABC *abc) {\n" check("void f(struct ABC *abc) {\n"
@ -2100,7 +2095,7 @@ private:
" foo(abc);\n" " foo(abc);\n"
" if (abc) { }\n" " if (abc) { }\n"
"}", true); "}", true);
ASSERT_EQUALS("[test.cpp:2]: (error, inconclusive) Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 4\n", errout.str()); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (error, inconclusive) Possible null pointer dereference: abc - otherwise it is redundant to check it against null.\n", errout.str());
} }
} }