From 43e6e6e28166741296e5b4974e7834641387c649 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Sat, 8 Jun 2024 19:58:29 +0200 Subject: [PATCH] Fix #12823 Crash in isVariableChanged() with unknown macro (#6494) --- lib/tokenize.cpp | 4 ++++ test/testsimplifytypedef.cpp | 9 +++++++++ test/testtokenize.cpp | 2 ++ 3 files changed, 15 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index c7435ec4cdd..ba80dc095c0 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -8706,6 +8706,8 @@ void Tokenizer::findGarbageCode() const if (!Token::Match(tok->next(), "%name%|*|~")) syntaxError(tok, tok->strAt(-1) + " " + tok->str() + " " + tok->strAt(1)); } + if (Token::Match(tok, "[{,] . %name%") && !Token::Match(tok->tokAt(3), "[.=[{]")) + syntaxError(tok->next()); if (Token::Match(tok, "[!|+-/%^~] )|]")) syntaxError(tok); if (Token::Match(tok, "==|!=|<=|>= %comp%") && tok->strAt(-1) != "operator") @@ -8760,6 +8762,8 @@ void Tokenizer::findGarbageCode() const if (!tok2->next() || tok2->isControlFlowKeyword() || Token::Match(tok2, "typedef|static|.")) syntaxError(tok); if (Token::Match(tok2, "%name% %name%") && tok2->str() == tok2->strAt(1)) { + if (Token::simpleMatch(tok2->tokAt(2), ";")) + continue; if (tok2->isStandardType() && tok2->str() == "long") continue; if (Token::Match(tok2->tokAt(-1), "enum|struct|union") || (isCPP() && Token::Match(tok2->tokAt(-1), "class|::"))) diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index a9ba778d635..6809a7d275d 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -218,6 +218,7 @@ class TestSimplifyTypedef : public TestFixture { TEST_CASE(simplifyTypedef151); TEST_CASE(simplifyTypedef152); TEST_CASE(simplifyTypedef153); + TEST_CASE(simplifyTypedef154); TEST_CASE(simplifyTypedefFunction1); TEST_CASE(simplifyTypedefFunction2); // ticket #1685 @@ -3605,6 +3606,14 @@ class TestSimplifyTypedef : public TestFixture { ASSERT_EQUALS(exp, tok(code)); } + void simplifyTypedef154() { + const char code[] = "typedef int T;\n" + "typedef T T;\n" + "T t = 0;\n"; + const char exp[] = "int t ; t = 0 ;"; + ASSERT_EQUALS(exp, tok(code)); + } + void simplifyTypedefFunction1() { { const char code[] = "typedef void (*my_func)();\n" diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 01952ab2f02..353b4f35998 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -7108,6 +7108,8 @@ class TestTokenizer : public TestFixture { // Ticket #9664 ASSERT_NO_THROW(tokenizeAndStringify("S s = { .x { 2 }, .y[0] { 3 } };")); + ASSERT_THROW_INTERNAL(tokenizeAndStringify("f(0, .x());"), SYNTAX); // #12823 + // Ticket #11134 ASSERT_NO_THROW(tokenizeAndStringify("struct my_struct { int x; }; " "std::string s; "