Fixed #8018: Tokenize::findGarbageCode: detect heads of for-loops with 1 or more than 2 semicolons as garbage code.

This commit is contained in:
Matthias Krüger 2017-05-01 14:10:06 +02:00
parent 19ce65217e
commit cee0f724ff
2 changed files with 24 additions and 2 deletions

View File

@ -8133,6 +8133,27 @@ const Token * Tokenizer::findGarbageCode() const
} }
} }
for (const Token *tok = tokens(); tok ; tok = tok->next()) {
if (!Token::simpleMatch(tok, "for (")) // find for loops
continue;
// count number of semicolons
unsigned int semicolons = 0;
const Token* const startTok = tok;
tok = tok->next()->link()->previous(); // find ")" of the for-loop
// walk backwards until we find the beginning (startTok) of the for() again
for (; tok != startTok; tok = tok->previous()) {
if (tok->str() == ";") { // do the counting
semicolons++;
} else if (tok->str() == ")") { // skip pairs of ( )
tok = tok->link();
}
}
// if we have an invalid number of semicolons inside for( ), assume syntax error
if ((semicolons == 1) || (semicolons > 2)) {
return tok;
}
}
// Code must not start with an arithmetical operand // Code must not start with an arithmetical operand
if (Token::Match(list.front(), "%cop%")) if (Token::Match(list.front(), "%cop%"))
return list.front(); return list.front();

View File

@ -1035,7 +1035,8 @@ private:
} }
void garbageCode136() { // #7033 void garbageCode136() { // #7033
checkCode("{ } () { void f() { node_t * n; for (; -n) {} } } { }"); ASSERT_THROW(checkCode("{ } () { void f() { node_t * n; for (; -n) {} } } { }"),
InternalError);
} }
void garbageCode137() { // #7034 void garbageCode137() { // #7034
@ -1326,7 +1327,7 @@ private:
void garbageCode165() { void garbageCode165() {
//7235 //7235
checkCode("for(;..)", false); ASSERT_THROW(checkCode("for(;..)", false),InternalError);
} }
void garbageCode167() { void garbageCode167() {