Fixed #1041 (passing a nullpointer to a reference)

This commit is contained in:
Daniel Marjamäki 2009-12-30 17:42:41 +01:00
parent a7ab47bb98
commit d83131ca92
3 changed files with 26 additions and 0 deletions

View File

@ -1275,6 +1275,19 @@ private:
bailOutVar(checks, tok.varId());
}
if (Token::simpleMatch(&tok, "* 0"))
{
if (Token::Match(tok.previous(), "[;{}=]"))
{
CheckOther *checkOther = dynamic_cast<CheckOther *>(owner);
if (checkOther)
{
checkOther->nullPointerError(&tok);
foundError = true;
}
}
}
return &tok;
}
@ -1867,6 +1880,11 @@ void CheckOther::strPlusChar(const Token *tok)
reportError(tok, Severity::error, "strPlusChar", "Unusual pointer arithmetic");
}
void CheckOther::nullPointerError(const Token *tok)
{
reportError(tok, Severity::error, "nullPointer", "Null pointer dereference");
}
void CheckOther::nullPointerError(const Token *tok, const std::string &varname)
{
reportError(tok, Severity::error, "nullPointer", "Possible null pointer dereference: " + varname);

View File

@ -147,6 +147,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); // variable name unknown / doesn't exist
void nullPointerError(const Token *tok, const std::string &varname);
void nullPointerError(const Token *tok, const std::string &varname, const int line);
void uninitdataError(const Token *tok, const std::string &varname);

View File

@ -915,6 +915,13 @@ private:
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Possible null pointer dereference: q\n", errout.str());
checkNullPointer("static void foo()\n"
"{\n"
" int *p = 0;\n"
" int &r = *p;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Null pointer dereference\n", errout.str());
// no false positive..
checkNullPointer("static void foo()\n"
"{\n"