From 3e22ef1c21f19548b2b2b7c35aa70840deff8308 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 4 Apr 2024 17:45:34 +0200 Subject: [PATCH] Fix #12586 Crash in getReallocFuncInfo() with namespace alias (#6230) --- lib/tokenize.cpp | 2 ++ test/testsimplifytokens.cpp | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 4dcd5b6fad5..2d3c97e418a 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -10605,6 +10605,8 @@ void Tokenizer::simplifyNamespaceAliases() } if (tok2->strAt(1) == "::" && !alreadyHasNamespace(tokNameStart, tokNameEnd, tok2)) { + if (Token::simpleMatch(tok2->tokAt(-1), "::") && tokNameStart->str() == "::") + tok2->deletePrevious(); tok2->str(tokNameStart->str()); Token * tok3 = tokNameStart; while (tok3 != tokNameEnd) { diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 3a14888eef4..0f46f1e3998 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -113,6 +113,7 @@ class TestSimplifyTokens : public TestFixture { TEST_CASE(simplifyNestedNamespace); TEST_CASE(simplifyNamespaceAliases1); TEST_CASE(simplifyNamespaceAliases2); // ticket #10281 + TEST_CASE(simplifyNamespaceAliases3); TEST_CASE(simplifyKnownVariables2); TEST_CASE(simplifyKnownVariables3); @@ -1502,6 +1503,18 @@ class TestSimplifyTokens : public TestFixture { "}")); } + void simplifyNamespaceAliases3() { + ASSERT_EQUALS("namespace N { namespace O { int g ( ) ; } } " // #12586 + "void f ( ) { " + "int s ; s = 3 * :: N :: O :: g ( ) ; " + "}", + tok("namespace N { namespace O { int g(); } }\n" + "namespace _n = ::N::O;\n" + "void f() {\n" + " int s = 3 * ::_n::g();\n" + "}\n")); + } + #define simplifyKnownVariables(code) simplifyKnownVariables_(code, __FILE__, __LINE__) std::string simplifyKnownVariables_(const char code[], const char* file, int line) { Tokenizer tokenizer(settings0, this);