Fixed #4569 (False Positive: Uninitialised variable)

This commit is contained in:
Daniel Marjamäki 2013-02-11 18:31:14 +01:00
parent 94f1d34dcb
commit 442db6cdf9
3 changed files with 14 additions and 4 deletions

View File

@ -1322,7 +1322,7 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
if (tok2 && tok2->str() == "{") {
bool possibleinit = false;
bool init = checkLoopBody(tok2, var, membervar);
bool init = checkLoopBody(tok2, var, membervar, suppressErrors);
// variable is initialized in the loop..
if (possibleinit || init)
@ -1435,7 +1435,7 @@ bool CheckUninitVar::checkIfForWhileHead(const Token *startparentheses, const Va
return false;
}
bool CheckUninitVar::checkLoopBody(const Token *tok, const Variable& var, const std::string &membervar)
bool CheckUninitVar::checkLoopBody(const Token *tok, const Variable& var, const std::string &membervar, const bool suppressErrors)
{
const Token *usetok = NULL;
@ -1458,7 +1458,7 @@ bool CheckUninitVar::checkLoopBody(const Token *tok, const Variable& var, const
}
}
if (usetok) {
if (!suppressErrors && usetok) {
if (membervar.empty())
uninitvarError(usetok, usetok->str());
else

View File

@ -66,7 +66,7 @@ public:
void checkScope(const Scope* scope);
bool checkScopeForVariable(const Scope* scope, const Token *tok, const Variable& var, bool * const possibleInit, bool * const noreturn, const std::string &membervar);
bool checkIfForWhileHead(const Token *startparentheses, const Variable& var, bool suppressErrors, bool isuninit, const std::string &membervar);
bool checkLoopBody(const Token *tok, const Variable& var, const std::string &membervar);
bool checkLoopBody(const Token *tok, const Variable& var, const std::string &membervar, const bool suppressErrors);
static bool isVariableUsage(const Token *vartok, bool ispointer, bool cpp);
bool isMemberVariableAssignment(const Token *tok, const std::string &membervar) const;
bool isMemberVariableUsage(const Token *tok, bool isPointer, const std::string &membervar) const;

View File

@ -2758,6 +2758,16 @@ private:
"}\n", "test.c");
ASSERT_EQUALS("[test.c:5]: (error) Uninitialized variable: ab\n"
"[test.c:5]: (error) Uninitialized struct member: ab.a\n", errout.str());
checkUninitVar2("void f(int i) {\n" // #4569 fp
" float *buffer;\n"
" if(i>10) buffer = f;\n"
" if(i>10) {\n"
" for (int i=0;i<10;i++)\n"
" buffer[i] = 0;\n" // <- fp
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void uninitvar2_4494() {