Skip to content

Commit

Permalink
Fix #12501 Crash in autoVariables() with local function declaration (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Mar 12, 2024
1 parent d49fd82 commit b555246
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand Down
16 changes: 16 additions & 0 deletions test/testvarid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit b555246

Please sign in to comment.