Refactoring: updated the nullPointer message so it contains the name of the pointer

This commit is contained in:
Daniel Marjamäki 2009-08-11 17:18:01 +02:00
parent ecdbcbce3d
commit e7feac506c
3 changed files with 22 additions and 14 deletions

View File

@ -961,6 +961,8 @@ void CheckOther::nullPointer()
if (varid == 0)
continue;
const std::string varname(tok->strAt(2));
// Locate the end of the while loop..
const Token *tok2 = tok->tokAt(4);
int indentlevel = 0;
@ -1001,7 +1003,7 @@ void CheckOther::nullPointer()
Token::Match(tok3->previous(), "[({};]") ||
tok3->previous()->isName())
{
nullPointerError(tok2);
nullPointerError(tok2, varname);
}
}
break;
@ -1045,6 +1047,8 @@ void CheckOther::nullPointer()
if (Token::Match(tok2->tokAt(-2), "%varid% ?", varid))
continue;
const std::string varname(tok2->str());
// Check usage of dereferenced variable in the loop..
unsigned int indentlevel3 = 0;
for (const Token *tok3 = tok1->next()->link(); tok3; tok3 = tok3->next())
@ -1069,7 +1073,7 @@ void CheckOther::nullPointer()
{
if (indentlevel4 <= 1)
{
nullPointerError(tok1);
nullPointerError(tok1, varname);
break;
}
--indentlevel4;
@ -1096,6 +1100,8 @@ void CheckOther::nullPointer()
if (varid1 == 0)
continue;
const std::string varname(tok1->str());
// Checking if the struct pointer is non-null before the assignment..
{
const Token *tok2 = _tokenizer->tokens();
@ -1144,7 +1150,7 @@ void CheckOther::nullPointer()
else if (Token::Match(tok2, "if ( !| %varid% )", varid1))
{
nullPointerError(tok1);
nullPointerError(tok1, varname);
break;
}
}
@ -1160,6 +1166,8 @@ void CheckOther::nullPointer()
if (varid == 0)
continue;
const std::string varname(tok->strAt(3));
const Token *decltok = Token::findmatch(_tokenizer->tokens(), "%varid%", varid);
if (!Token::Match(decltok->tokAt(-3), "[;,(] %var% *"))
continue;
@ -1170,7 +1178,7 @@ void CheckOther::nullPointer()
{
if (tok1->previous() && tok1->previous()->str() == "*" && tok1->tokAt(-2)->str() != "*")
{
nullPointerError(tok1);
nullPointerError(tok1, varname);
break;
}
else if (tok1->previous() && tok1->previous()->str() == "&")
@ -1318,9 +1326,9 @@ void CheckOther::strPlusChar(const Token *tok)
reportError(tok, Severity::error, "strPlusChar", "Unusual pointer arithmetic");
}
void CheckOther::nullPointerError(const Token *tok)
void CheckOther::nullPointerError(const Token *tok, const std::string &varname)
{
reportError(tok, Severity::error, "nullPointer", "Possible null pointer dereference");
reportError(tok, Severity::error, "nullPointer", "Possible null pointer dereference: " + varname);
}
void CheckOther::zerodivError(const Token *tok)

View File

@ -142,7 +142,7 @@ public:
void variableScopeError(const Token *tok, const std::string &varname);
void conditionAlwaysTrueFalse(const Token *tok, const std::string &truefalse);
void strPlusChar(const Token *tok);
void nullPointerError(const Token *tok);
void nullPointerError(const Token *tok, const std::string &varname);
void zerodivError(const Token *tok);
void postIncrementError(const Token *tok, const std::string &var_name, const bool isIncrement);
@ -156,15 +156,15 @@ public:
sprintfOverlappingDataError(0, "varname");
udivError(0);
udivWarning(0);
unusedStructMemberError(0, "structname", "varname");
passedByValueError(0, "parname");
unusedStructMemberError(0, "structname", "variable");
passedByValueError(0, "parametername");
constStatementError(0, "type");
charArrayIndexError(0);
charBitOpError(0);
variableScopeError(0, "varname");
conditionAlwaysTrueFalse(0, "true/false");
strPlusChar(0);
nullPointerError(0);
nullPointerError(0, "pointer");
zerodivError(0);
postIncrementError(0, "varname", true);
}

View File

@ -435,7 +435,7 @@ private:
" while (tok);\n"
" tok = tok->next();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference\n", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: tok\n", errout.str());
checkNullPointer("void foo()\n"
"{\n"
@ -445,7 +445,7 @@ private:
" tok = tok->next();\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference\n", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: tok\n", errout.str());
checkNullPointer("void foo()\n"
"{\n"
@ -480,7 +480,7 @@ private:
" if (!abc)\n"
" ;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference\n", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: abc\n", errout.str());
// ok dereferencing in a condition
checkNullPointer("void foo(struct ABC *abc)\n"
@ -568,7 +568,7 @@ private:
" if (!p)\n"
" ;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference\n", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p\n", errout.str());
// no error
checkNullPointer("void foo()\n"