Fixed ticket #3482 (segmentation fault of cppcheck ( switch(){case} ))

This commit is contained in:
Edoardo Prezioso 2012-01-09 14:37:20 +01:00
parent 57fcde8090
commit 54f54761b4
2 changed files with 40 additions and 26 deletions

View File

@ -2694,36 +2694,48 @@ void Tokenizer::arraySize()
void Tokenizer::simplifyLabelsCaseDefault()
{
bool executablescope = false;
unsigned int indentlevel = 0;
for (Token *tok = _tokens; tok; tok = tok->next()) {
// Simplify labels in the executable scope..
if (Token::Match(tok, ") const| {")) {
// Simplify labels in the executable scope..
unsigned int indentlevel = 0;
while (NULL != (tok = tok->next())) {
if (tok->str() == "{") {
if (tok->previous() && tok->previous()->str() == "=")
tok = tok->link();
else
++indentlevel;
} else if (tok->str() == "}") {
--indentlevel;
if (!indentlevel)
break;
} else if (tok->str() == "(" || tok->str() == "[")
tok = tok->link();
tok = tok->next();
if (tok->str() == "const")
tok = tok->next();
executablescope = true;
}
if (Token::Match(tok, "[;{}] case")) {
while (NULL != (tok = tok->next())) {
if (tok->str() == ":")
break;
}
if (Token::Match(tok, ": !!;")) {
tok->insertToken(";");
}
} else if (Token::Match(tok, "[;{}] %var% : !!;")) {
tok = tok->tokAt(2);
tok->insertToken(";");
}
if (!executablescope)
continue;
if (tok->str() == "{") {
if (tok->previous()->str() == "=")
tok = tok->link();
else
++indentlevel;
} else if (tok->str() == "}") {
--indentlevel;
if (!indentlevel) {
executablescope = false;
continue;
}
} else if (tok->str() == "(" || tok->str() == "[")
tok = tok->link();
if (Token::Match(tok, "[;{}] case")) {
while (NULL != (tok = tok->next())) {
if (tok->str() == ":")
break;
}
if (!tok)
break;
else if (tok->str() == ":" &&
(!tok->next() || tok->next()->str() != ";")) {
tok->insertToken(";");
}
} else if (Token::Match(tok, "[;{}] %var% : !!;")) {
tok = tok->tokAt(2);
tok->insertToken(";");
}
}
}

View File

@ -5239,6 +5239,8 @@ private:
//with unhandled MACRO() code
ASSERT_EQUALS(" void f(){ MACRO( ab: b=0;, foo)}", labels_("void f() { MACRO(ab: b=0;, foo)}"));
ASSERT_EQUALS(" void f(){ MACRO( bar, ab:{&(* b. x)=0;})}", labels_("void f() { MACRO(bar, ab: {&(*b.x)=0;})}"));
//don't crash with garbage code
ASSERT_EQUALS(" switch(){ case}", labels_("switch(){case}"));
}
// Check simplifyInitVar