Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #11643 Tokenizer: garbage code 'for (()())' #6120

Merged
merged 4 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8532,13 +8532,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 @@ -8548,6 +8550,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
2 changes: 1 addition & 1 deletion test/testgarbage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ class TestGarbage : public TestFixture {
}

void garbageCode142() { // #7050
checkCode("{ } ( ) { void mapGraphs ( ) { node_t * n ; for (!oid n ) { } } } { }");
ASSERT_THROW(checkCode("{ } ( ) { void mapGraphs ( ) { node_t * n ; for (!oid n ) { } } } { }"), InternalError);
}

void garbageCode143() { // #6922
Expand Down
2 changes: 2 additions & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7126,6 +7126,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
Loading