Fixed ticket #3486 (segmentation fault of cppcheck)

This commit is contained in:
Edoardo Prezioso 2012-01-13 23:30:43 +01:00
parent 0648b3ed5e
commit 37f3aa1528
2 changed files with 47 additions and 31 deletions

View File

@ -1120,33 +1120,37 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const unsigned int
// goto the { // goto the {
tok = tok->next()->link()->next(); tok = tok->next()->link()->next();
bool possibleInitIf(number_of_if > 0 || suppressErrors); if (!tok)
const bool initif = checkScopeForVariable(tok->next(), varid, ispointer, &possibleInitIf); break;
if (tok->str() == "{") {
// goto the } bool possibleInitIf(number_of_if > 0 || suppressErrors);
tok = tok->link(); const bool initif = checkScopeForVariable(tok->next(), varid, ispointer, &possibleInitIf);
if (!Token::simpleMatch(tok, "} else {")) {
if (initif || possibleInitIf) {
++number_of_if;
if (number_of_if >= 2)
return true;
}
} else {
// goto the {
tok = tok->tokAt(2);
bool possibleInitElse(number_of_if > 0 || suppressErrors);
const bool initelse = checkScopeForVariable(tok->next(), varid, ispointer, &possibleInitElse);
// goto the } // goto the }
tok = tok->link(); tok = tok->link();
if (initif && initelse) if (!Token::simpleMatch(tok, "} else {")) {
return true; if (initif || possibleInitIf) {
++number_of_if;
if (number_of_if >= 2)
return true;
}
} else {
// goto the {
tok = tok->tokAt(2);
if (initif || initelse || possibleInitElse) bool possibleInitElse(number_of_if > 0 || suppressErrors);
++number_of_if; const bool initelse = checkScopeForVariable(tok->next(), varid, ispointer, &possibleInitElse);
// goto the }
tok = tok->link();
if (initif && initelse)
return true;
if (initif || initelse || possibleInitElse)
++number_of_if;
}
} }
} }
@ -1177,16 +1181,18 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const unsigned int
// goto the { // goto the {
const Token *tok2 = tok->next()->link()->next(); const Token *tok2 = tok->next()->link()->next();
bool possibleinit = true; if (tok2 && tok2->str() == "{") {
bool init = checkScopeForVariable(tok2->next(), varid, ispointer, &possibleinit); bool possibleinit = true;
bool init = checkScopeForVariable(tok2->next(), varid, ispointer, &possibleinit);
// variable is initialized in the loop.. // variable is initialized in the loop..
if (possibleinit || init) if (possibleinit || init)
return true; return true;
// is variable used in for-head? // is variable used in for-head?
if (!suppressErrors) { if (!suppressErrors) {
checkIfForWhileHead(tok->next(), varid, ispointer, false, bool(number_of_if == 0)); checkIfForWhileHead(tok->next(), varid, ispointer, false, bool(number_of_if == 0));
}
} }
} }
@ -1206,7 +1212,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const unsigned int
return true; return true;
// variable is seen.. // variable is seen..
if (tok->varId() == varid) { if (tok && tok->varId() == varid) {
// Use variable // Use variable
if (!suppressErrors && isVariableUsage(tok, ispointer)) if (!suppressErrors && isVariableUsage(tok, ispointer))
uninitvarError(tok, tok->str()); uninitvarError(tok, tok->str());

View File

@ -2019,6 +2019,16 @@ private:
" typeof(*abc);\n" " typeof(*abc);\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// Ticket #3486 - Don't crash garbage code
checkUninitVar2("void f()\n"
"{\n"
" (\n"
" x;\n"
" int a, a2, a2*x; if () ;\n"
" )\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: a2\n", errout.str());
} }
}; };