Ticket #5214: Don't read out of bounds upon invalid ternary operator.

This commit is contained in:
Simon Martin 2013-12-21 08:08:19 +01:00
parent e2bc99aa24
commit 02dc49624d
2 changed files with 14 additions and 1 deletions

View File

@ -2566,6 +2566,8 @@ static Token *skipTernaryOp(Token *tok)
if (Token::Match(tok->next(), "[{};]")) if (Token::Match(tok->next(), "[{};]"))
break; break;
} }
if (colonlevel) // Ticket #5214: Make sure the ':' matches the proper '?'
return 0;
return tok; return tok;
} }
@ -2606,7 +2608,12 @@ bool Tokenizer::simplifyLabelsCaseDefault()
if (tok->str() == "(" || tok->str() == "[") { if (tok->str() == "(" || tok->str() == "[") {
tok = tok->link(); tok = tok->link();
} else if (tok->str() == "?") { } else if (tok->str() == "?") {
tok = skipTernaryOp(tok); Token *tok1 = skipTernaryOp(tok);
if(!tok1) {
syntaxError(tok);
return false;
}
tok = tok1;
} }
if (Token::Match(tok->next(),"[:{};]")) if (Token::Match(tok->next(),"[:{};]"))
break; break;

View File

@ -75,6 +75,7 @@ private:
TEST_CASE(garbageCode3); // #4869 TEST_CASE(garbageCode3); // #4869
TEST_CASE(garbageCode4); // #4887 TEST_CASE(garbageCode4); // #4887
TEST_CASE(garbageCode5); // #5168 TEST_CASE(garbageCode5); // #5168
TEST_CASE(garbageCode6); // #5214
TEST_CASE(simplifyFileAndLineMacro); // tokenize "return - __LINE__;" TEST_CASE(simplifyFileAndLineMacro); // tokenize "return - __LINE__;"
@ -982,6 +983,11 @@ private:
tokenizeAndStringify("( asm : ; void : );"); tokenizeAndStringify("( asm : ; void : );");
} }
void garbageCode6() { // #5214
tokenizeAndStringify("int b = ( 0 ? ? ) 1 : 0 ;", /*simplify=*/true);
tokenizeAndStringify("int a = int b = ( 0 ? ? ) 1 : 0 ;", /*simplify=*/true);
}
void simplifyFileAndLineMacro() { // tokenize 'return - __LINE__' correctly void simplifyFileAndLineMacro() { // tokenize 'return - __LINE__' correctly
ASSERT_EQUALS("\"test.cpp\"", tokenizeAndStringify("__FILE__")); ASSERT_EQUALS("\"test.cpp\"", tokenizeAndStringify("__FILE__"));
ASSERT_EQUALS("return -1 ;", tokenizeAndStringify("return - __LINE__;")); ASSERT_EQUALS("return -1 ;", tokenizeAndStringify("return - __LINE__;"));