uninitialized data: added more checking for allocated data

This commit is contained in:
Daniel Marjamäki 2009-11-07 09:10:15 +01:00
parent 0c8d327809
commit 60caefe069
3 changed files with 44 additions and 2 deletions

View File

@ -1412,7 +1412,33 @@ void CheckOther::uninitvar()
break; break;
--indentlevel; --indentlevel;
} }
if (Token::Match(tok, "[{};] %type% *| %var% [;[]"))
if (Token::Match(tok, "[{};] %var% = malloc|kmalloc (") ||
Token::Match(tok, "[{};] %var% = new char [") )
{
// check that the variable id is non-zero
const unsigned int varid = tok->next()->varId();
if (varid == 0)
continue;
// goto ')'
tok = tok->tokAt(4)->link();
if (!tok)
break;
// check if data is accessed uninitialized..
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next())
{
if (tok2->str() == "{" || tok2->str() == "}")
break;
if (tok2->varId() == varid)
break;
if (Token::Match(tok2, "strcat|strncat|strlen ( %varid%", varid))
uninitdataError(tok2, tok2->strAt(2));
}
}
else if (Token::Match(tok, "[{};] %type% *| %var% [;[]"))
{ {
if (Token::Match(tok->next(), "return|goto")) if (Token::Match(tok->next(), "return|goto"))
continue; continue;
@ -1594,6 +1620,11 @@ void CheckOther::nullPointerError(const Token *tok, const std::string &varname,
reportError(tok, Severity::error, "nullPointer", "Possible null pointer dereference: " + varname + " - otherwise it is redundant to check if " + varname + " is null at line " + MathLib::toString<long>(line)); reportError(tok, Severity::error, "nullPointer", "Possible null pointer dereference: " + varname + " - otherwise it is redundant to check if " + varname + " is null at line " + MathLib::toString<long>(line));
} }
void CheckOther::uninitdataError(const Token *tok, const std::string &varname)
{
reportError(tok, Severity::error, "uninitdata", "Data is allocated but not initialized: " + varname);
}
void CheckOther::uninitvarError(const Token *tok, const std::string &varname) void CheckOther::uninitvarError(const Token *tok, const std::string &varname)
{ {
reportError(tok, Severity::error, "uninitvar", "Uninitialized variable: " + varname); reportError(tok, Severity::error, "uninitvar", "Uninitialized variable: " + varname);

View File

@ -147,6 +147,7 @@ public:
void strPlusChar(const Token *tok); void strPlusChar(const Token *tok);
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 int line); void nullPointerError(const Token *tok, const std::string &varname, const int line);
void uninitdataError(const Token *tok, const std::string &varname);
void uninitvarError(const Token *tok, const std::string &varname); void uninitvarError(const Token *tok, const std::string &varname);
void zerodivError(const Token *tok); void zerodivError(const Token *tok);
void postIncrementError(const Token *tok, const std::string &var_name, const bool isIncrement); void postIncrementError(const Token *tok, const std::string &var_name, const bool isIncrement);
@ -157,6 +158,7 @@ public:
sprintfOverlappingDataError(0, "varname"); sprintfOverlappingDataError(0, "varname");
udivError(0); udivError(0);
nullPointerError(0, "pointer"); nullPointerError(0, "pointer");
uninitdataError(0, "varname");
uninitvarError(0, "varname"); uninitvarError(0, "varname");
zerodivError(0); zerodivError(0);
@ -192,6 +194,7 @@ public:
" * [[OverlappingData|bad usage of the function 'sprintf' (overlapping data)]]\n" " * [[OverlappingData|bad usage of the function 'sprintf' (overlapping data)]]\n"
" * division with zero\n" " * division with zero\n"
" * null pointer dereferencing\n" " * null pointer dereferencing\n"
" * using uninitialized variables and data\n"
// style // style
" * C-style pointer cast in cpp file\n" " * C-style pointer cast in cpp file\n"
@ -205,7 +208,6 @@ public:
" * variable scope can be limited\n" " * variable scope can be limited\n"
" * condition that is always true/false\n" " * condition that is always true/false\n"
" * unusal pointer arithmetic. For example: \"abc\" + 'd'\n" " * unusal pointer arithmetic. For example: \"abc\" + 'd'\n"
" * uninitialized variables\n"
// optimisations // optimisations
" * optimisation: detect post increment/decrement\n"; " * optimisation: detect post increment/decrement\n";

View File

@ -1136,6 +1136,15 @@ private:
" strcat(s, \"abc\");\n" " strcat(s, \"abc\");\n"
"};\n"); "};\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: s\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: s\n", errout.str());
// alloc..
checkUninitVar("void f()\n"
"{\n"
" char *s = malloc(100);\n"
" strcat(s, \"abc\");\n"
"};\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Data is allocated but not initialized: s\n", errout.str());
} }