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 all 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 @@ -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
Loading