From b555246374f6c8eb27160ea21dc7c30ac0fc4b7a Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 12 Mar 2024 07:59:39 +0100 Subject: [PATCH] Fix #12501 Crash in autoVariables() with local function declaration (#6114) --- lib/tokenize.cpp | 4 ++-- test/testvarid.cpp | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 6a8d07e2d66..4f44688aca0 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4824,7 +4824,7 @@ void Tokenizer::setVarIdPass1() } // function declaration inside executable scope? Function declaration is of form: type name "(" args ")" - if (scopeStack.top().isExecutable && Token::Match(tok, "%name% [,)]")) { + if (scopeStack.top().isExecutable && Token::Match(tok, "%name% [,)[]")) { bool par = false; const Token *start, *end; @@ -4844,7 +4844,7 @@ void Tokenizer::setVarIdPass1() } // search end of function declaration - for (end = tok->next(); Token::Match(end, "%name%|*|&|,"); end = end->next()) {} + for (end = tok->next(); Token::Match(end, "%name%|*|&|,|[|]|%num%"); end = end->next()) {} // there are tokens which can't appear at the begin of a function declaration such as "return" const bool isNotstartKeyword = start->next() && notstart.find(start->next()->str()) != notstart.end(); diff --git a/test/testvarid.cpp b/test/testvarid.cpp index 727035b9c64..2383658eec7 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -1497,6 +1497,22 @@ class TestVarID : public TestFixture { const char expected[] = "1: bool f ( X x@1 , int = 3 ) ;\n"; ASSERT_EQUALS(expected, actual); } + + { + const std::string actual = tokenize("int main() {\n" + " int a[2];\n" + " extern void f(int a[2]);\n" + " f(a);\n" + " a[0] = 0;\n" + "}\n", "test.cpp"); + const char expected[] = "1: int main ( ) {\n" + "2: int a@1 [ 2 ] ;\n" + "3: extern void f ( int a [ 2 ] ) ;\n" + "4: f ( a@1 ) ;\n" + "5: a@1 [ 0 ] = 0 ;\n" + "6: }\n"; + ASSERT_EQUALS(expected, actual); + } } void varid_sizeof() {