Skip to content

Commit

Permalink
Fix #11643 Tokenizer: garbage code 'for (()())' (#6120)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Mar 13, 2024
1 parent 1bace58 commit 4eac07f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
6 changes: 5 additions & 1 deletion lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8537,13 +8537,15 @@ void Tokenizer::findGarbageCode() const
if (!Token::simpleMatch(tok, "for (")) // find for loops
continue;
// count number of semicolons
int semicolons = 0;
int semicolons = 0, colons = 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() == ":") {
colons++;
} else if (tok->str() == ")") { // skip pairs of ( )
tok = tok->link();
}
Expand All @@ -8553,6 +8555,8 @@ void Tokenizer::findGarbageCode() const
syntaxError(tok);
if (semicolons == 1 && !(isCPP() && mSettings.standards.cpp >= Standards::CPP20))
syntaxError(tok);
if (semicolons == 0 && colons == 0)
syntaxError(tok);
}

// Operators without operands..
Expand Down
6 changes: 2 additions & 4 deletions test/testgarbage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1174,8 +1174,7 @@ class TestGarbage : public TestFixture {
}

void garbageCode142() { // #7050
checkCode("{ } ( ) { void mapGraphs ( ) { node_t * n ; for (!oid n ) { } } } { }");
(void)errout_str(); // we are not interested in the output
ASSERT_THROW(checkCode("{ } ( ) { void mapGraphs ( ) { node_t * n ; for (!oid n ) { } } } { }"), InternalError);
}

void garbageCode143() { // #6922
Expand Down Expand Up @@ -1312,8 +1311,7 @@ class TestGarbage : public TestFixture {
ASSERT_THROW(checkCode(code), InternalError);

code = "void f1() { for (int n = 0 n < 10 n++); }";
checkCode(code);
(void)errout_str(); // we are not interested in the output
ASSERT_THROW(checkCode(code), InternalError);
}

void garbageSymbolDatabase() {
Expand Down
2 changes: 2 additions & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7048,6 +7048,8 @@ class TestTokenizer : public TestFixture {
ASSERT_THROW_EQUALS(tokenizeAndStringify("int (*f) MACRO((void *));\n"), // #12010
InternalError,
"There is an unknown macro here somewhere. Configuration is required. If MACRO is a macro then please configure it.");

ASSERT_THROW(tokenizeAndStringify("{ for (()()) }"), InternalError); // #11643
}


Expand Down

0 comments on commit 4eac07f

Please sign in to comment.