Fixed ticket #3480 (segmentation fault of cppcheck)

This commit is contained in:
Edoardo Prezioso 2012-01-09 12:36:05 +01:00
parent 194327048a
commit a951d34aa8
2 changed files with 25 additions and 20 deletions

View File

@ -300,9 +300,7 @@ void ExecutionPath::checkScope(const Token *tok, std::list<ExecutionPath *> &che
// bailout used variables in '; FOREACH ( .. ) { .. }' // bailout used variables in '; FOREACH ( .. ) { .. }'
else if (tok->str() != "if" && Token::Match(tok->previous(), "[;{}] %var% (")) { else if (tok->str() != "if" && Token::Match(tok->previous(), "[;{}] %var% (")) {
// goto { // goto {
const Token *tok2 = tok->next()->link(); const Token *tok2 = tok->next()->link()->next();
if (tok2 && tok2->str() == ")") {
tok2 = tok2->next();
if (tok2 && tok2->str() == "{") { if (tok2 && tok2->str() == "{") {
// goto "}" // goto "}"
tok2 = tok2->link(); tok2 = tok2->link();
@ -314,15 +312,15 @@ void ExecutionPath::checkScope(const Token *tok, std::list<ExecutionPath *> &che
} }
} }
} }
}
// .. ) { ... } => bail out // .. ) { ... } => bail out
if (Token::simpleMatch(tok, ") {")) { if (tok->str() == ")" && tok->next() && tok->next()->str() == "{") {
ExecutionPath::bailOut(checks); ExecutionPath::bailOut(checks);
return; return;
} }
if (Token::Match(tok, "abort|exit (")) { if ((tok->str() == "abort" || tok->str() == "exit") &&
tok->next() && tok->next()->str() == "(") {
ExecutionPath::bailOut(checks); ExecutionPath::bailOut(checks);
return; return;
} }
@ -360,12 +358,12 @@ void ExecutionPath::checkScope(const Token *tok, std::list<ExecutionPath *> &che
continue; continue;
} }
if (tok->str() == "if") { if (tok->str() == "if" && tok->next() && tok->next()->str() == "(") {
// what variable ids should the numberOfIf be counted for? // what variable ids should the numberOfIf be counted for?
std::set<unsigned int> countif; std::set<unsigned int> countif;
std::list<ExecutionPath *> newchecks; std::list<ExecutionPath *> newchecks;
while (tok->str() == "if") { while (tok->str() == "if" && tok->next() && tok->next()->str() == "(") {
// goto "(" // goto "("
tok = tok->next(); tok = tok->next();
@ -377,12 +375,12 @@ void ExecutionPath::checkScope(const Token *tok, std::list<ExecutionPath *> &che
} }
// goto ")" // goto ")"
tok = tok ? tok->link() : 0; tok = tok->link();
// goto "{" // goto "{"
tok = tok ? tok->next() : 0; tok = tok->next();
if (!Token::simpleMatch(tok, "{")) { if (!tok || tok->str() != "{") {
ExecutionPath::bailOut(checks); ExecutionPath::bailOut(checks);
ExecutionPath::bailOut(newchecks); ExecutionPath::bailOut(newchecks);
return; return;
@ -395,12 +393,12 @@ void ExecutionPath::checkScope(const Token *tok, std::list<ExecutionPath *> &che
tok = tok->link(); tok = tok->link();
// there is no else => break out // there is no else => break out
if (Token::Match(tok, "} !!else")) if (!tok->next() || tok->next()->str() != "else")
break; break;
// parse next "if".. // parse next "if"..
tok = tok->tokAt(2); tok = tok->tokAt(2);
if (tok->str() == "if") if (tok && tok->str() == "if")
continue; continue;
// there is no "if".. // there is no "if"..

View File

@ -527,6 +527,13 @@ private:
" return x() ? i : 0;\n" " return x() ? i : 0;\n"
"}\n"); "}\n");
TODO_ASSERT_EQUALS("[test.cpp:2]: (error) Uninitialized variable: i\n", "", errout.str()); TODO_ASSERT_EQUALS("[test.cpp:2]: (error) Uninitialized variable: i\n", "", errout.str());
// Ticket #3480 - Don't crash garbage code
checkUninitVar("int f()\n"
"{\n"
" return if\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }