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,20 +2694,31 @@ void Tokenizer::arraySize()
void Tokenizer::simplifyLabelsCaseDefault() void Tokenizer::simplifyLabelsCaseDefault()
{ {
for (Token *tok = _tokens; tok; tok = tok->next()) { bool executablescope = false;
if (Token::Match(tok, ") const| {")) {
// Simplify labels in the executable scope..
unsigned int indentlevel = 0; unsigned int indentlevel = 0;
while (NULL != (tok = tok->next())) { for (Token *tok = _tokens; tok; tok = tok->next()) {
// Simplify labels in the executable scope..
if (Token::Match(tok, ") const| {")) {
tok = tok->next();
if (tok->str() == "const")
tok = tok->next();
executablescope = true;
}
if (!executablescope)
continue;
if (tok->str() == "{") { if (tok->str() == "{") {
if (tok->previous() && tok->previous()->str() == "=") if (tok->previous()->str() == "=")
tok = tok->link(); tok = tok->link();
else else
++indentlevel; ++indentlevel;
} else if (tok->str() == "}") { } else if (tok->str() == "}") {
--indentlevel; --indentlevel;
if (!indentlevel) if (!indentlevel) {
break; executablescope = false;
continue;
}
} else if (tok->str() == "(" || tok->str() == "[") } else if (tok->str() == "(" || tok->str() == "[")
tok = tok->link(); tok = tok->link();
@ -2716,7 +2727,10 @@ void Tokenizer::simplifyLabelsCaseDefault()
if (tok->str() == ":") if (tok->str() == ":")
break; break;
} }
if (Token::Match(tok, ": !!;")) { if (!tok)
break;
else if (tok->str() == ":" &&
(!tok->next() || tok->next()->str() != ";")) {
tok->insertToken(";"); tok->insertToken(";");
} }
} else if (Token::Match(tok, "[;{}] %var% : !!;")) { } else if (Token::Match(tok, "[;{}] %var% : !!;")) {
@ -2724,8 +2738,6 @@ void Tokenizer::simplifyLabelsCaseDefault()
tok->insertToken(";"); tok->insertToken(";");
} }
} }
}
}
} }

View File

@ -5239,6 +5239,8 @@ private:
//with unhandled MACRO() code //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( 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;})}")); 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 // Check simplifyInitVar