From f2faf2ee30852a5a6018091f085b704536581130 Mon Sep 17 00:00:00 2001 From: chrchr Date: Wed, 13 Mar 2024 10:59:30 +0100 Subject: [PATCH 1/3] Fix #11643 Tokenizer: garbage code 'for (()())' --- lib/tokenize.cpp | 6 +++++- test/testtokenize.cpp | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index da1a6fd0456..fad68b11614 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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(); } @@ -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.. diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 33fb3228954..acf8699fac9 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -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 } From c1e12b785031ab430b3668f52f5032a18c3a11bf Mon Sep 17 00:00:00 2001 From: chrchr Date: Wed, 13 Mar 2024 11:07:36 +0100 Subject: [PATCH 2/3] Fix --- test/testgarbage.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testgarbage.cpp b/test/testgarbage.cpp index 639b931e7e0..5413711e653 100644 --- a/test/testgarbage.cpp +++ b/test/testgarbage.cpp @@ -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 From 304050baa9d5c139cffb57f5be8d2965213b749f Mon Sep 17 00:00:00 2001 From: chrchr Date: Wed, 13 Mar 2024 12:27:03 +0100 Subject: [PATCH 3/3] Fix another test --- test/testgarbage.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testgarbage.cpp b/test/testgarbage.cpp index 5413711e653..cb89b259442 100644 --- a/test/testgarbage.cpp +++ b/test/testgarbage.cpp @@ -1296,7 +1296,7 @@ class TestGarbage : public TestFixture { ASSERT_THROW(checkCode(code), InternalError); code = "void f1() { for (int n = 0 n < 10 n++); }"; - checkCode(code); + ASSERT_THROW(checkCode(code), InternalError); } void garbageSymbolDatabase() {