Fixed crashs on garbage code (#5604)

This commit is contained in:
PKEuS 2014-03-30 11:38:01 +02:00
parent aa527cb408
commit 060f9035c9
2 changed files with 24 additions and 7 deletions

View File

@ -2562,10 +2562,12 @@ void Tokenizer::setVarId()
if (tokStart) {
for (const Token *tok2 = tokStart->next(); tok2 != tokStart->link(); tok2 = tok2->next()) {
// skip parentheses..
if (tok2->str() == "{")
tok2 = tok2->link();
else if (tok2->str() == "(")
tok2 = tok2->link();
if (tok2->link()) {
if (tok2->str() == "{")
tok2 = tok2->link();
else if (tok2->str() == "(")
tok2 = tok2->link();
}
// Found a member variable..
else if (tok2->varId() > 0)
@ -7575,9 +7577,11 @@ void Tokenizer::simplifyEnum()
// are there shadow variables in the scope?
std::set<std::string> shadowVars;
for (const Token *tok3 = tok2->next(); tok3 && tok3->str() != "}"; tok3 = tok3->next()) {
if (tok3->str() == "{")
if (tok3->str() == "{") {
tok3 = tok3->link(); // skip inner scopes
else if (tok3->isName() && enumValues.find(tok3->str()) != enumValues.end()) {
if (tok3 == nullptr)
break;
} else if (tok3->isName() && enumValues.find(tok3->str()) != enumValues.end()) {
const Token *prev = tok3->previous();
if ((prev->isName() && !Token::Match(prev,"return|case|throw")) ||
Token::Match(prev, "&|* %type% =")) {

View File

@ -80,6 +80,7 @@ private:
TEST_CASE(garbageCode6); // #5214
TEST_CASE(garbageCode7);
TEST_CASE(garbageCode8); // #5511
TEST_CASE(garbageCode9); // #5604
TEST_CASE(simplifyFileAndLineMacro); // tokenize "return - __LINE__;"
@ -1030,8 +1031,20 @@ private:
tokenizeAndStringify("foo(Args&&...) fn void = { } auto template<typename... bar(Args&&...)", /*simplify=*/true);
}
void garbageCode8() {
void garbageCode8() { // #5604
ASSERT_THROW(tokenizeAndStringify("{ enum struct : };", true), InternalError);
ASSERT_THROW(tokenizeAndStringify("int ScopedEnum{ template<typename T> { { e = T::error }; };\n"
"ScopedEnum1<int> se1; { enum class E : T { e = 0 = e ScopedEnum2<void*> struct UnscopedEnum3 { T{ e = 4 }; };\n"
"arr[(int) E::e]; }; UnscopedEnum3<int> e2 = f()\n"
"{ { e = e1; T::error } int test1 ue2; g() { enum class E { e = T::error }; return E::e; } int test2 = } \n"
"namespace UnscopedEnum { template<typename T> struct UnscopedEnum1 { E{ e = T::error }; }; UnscopedEnum1<int> { enum E : { e = 0 }; };\n"
"UnscopedEnum2<void*> ue3; template<typename T> struct UnscopedEnum3 { enum { }; }; int arr[E::e]; };\n"
"UnscopedEnum3<int> namespace template<typename T> int f() { enum E { e }; T::error }; return (int) E(); } int test1 int g() { enum E { e = E };\n"
"E::e; } int test2 = g<int>(); }", true), InternalError);
}
void garbageCode9() {
ASSERT_THROW(tokenizeAndStringify("enum { e = { } } ( ) { { enum { } } } { e } ", true), InternalError);
}
void simplifyFileAndLineMacro() { // tokenize 'return - __LINE__' correctly