Skip to content

Fix #15245 syntaxError on new[] with a braced initializer ending in a parenthesis - #9087

Open
tsobczynski wants to merge 1 commit into
cppcheck-opensource:mainfrom
tsobczynski:fix-15245-knr-tokend
Open

tsobczynski wants to merge 1 commit into
cppcheck-opensource:mainfrom
tsobczynski:fix-15245-knr-tokend

Conversation

@tsobczynski

Copy link
Copy Markdown

In Tokenizer::simplifyVarDecl, the K&R parameter pass no longer steps onto tokEnd when it skips a parenthesised group, which made the loop run past the end of its range. Test: vardecl38.

🤖 Generated with Claude Code

… parenthesis

In the K&R parameter pass, simplifyVarDecl skips a parenthesised group
and steps past its `)`. When that `)` is the last token before tokEnd,
the step lands on tokEnd and the loop increment then steps past it, so
the loop runs to the end of the token list and raises a syntax error.
findLambdaEndScope takes `[2]{(1)}` in `new int[2]{(1)}` for a lambda,
which produces such a range.

Don't step onto tokEnd.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

I found no problems. The new tok->next() != tokEnd check keeps the K&R pass of simplifyVarDecl from stepping onto tokEnd, so the loop increment can no longer go past the end of the range. When tok->next() is tokEnd, the continue makes the next increment land on tokEnd and the loop stops. So the tokens that are still visited are the same as before. The fix treats the symptom: findLambdaEndScope still takes [2]{(1)} for a lambda, but that no longer causes a syntax error. For every other input I tried, the results are the same as base.

Testing

  • I built cppcheck and testrunner in pr/, and cppcheck in base/.
  • ./testrunner TestTokenizer TestGarbage TestVarID TestSimplifyTokens passes, including the new vardecl38. The full ./testrunner -q run passes too: 5428 tests, 0 failed.
  • The case of the PR, int* q{new int[2]{(1)}};:
    • base gives t1.cpp:1:19: error: syntax error [syntaxError]
    • the PR gives no syntax error.
  • I ran cppcheck -q --enable=all from base and from the PR on these variations and compared the output:
    • {((1))}, {(1), (2)}, {1,(2)}, {f(1)}, {(int)1} and {[](){return 1;}()} inside new int[..]
    • a member initialiser struct S { int* p{new int[3]{(1)}}; };
    • std::vector<int*> v{new int[1]{(0)}}
    • a C file with a K&R function.
  • On these files:
    • Every file except the PR case gives the same output in base and the PR. The PR case is only syntax-checked in base, which gives the syntax error above.
    • In the PR, the checks after the formerly failing declaration still run: memleak, uninitvar and unreadVariable are reported.
    • int a = 0, b = 1; that follows the formerly failing declaration is still split by simplifyVarDecl, as --debug-normal shows.
  • I did not find an input that the PR handles wrongly.

Automated review of ab3007f by Claude. The pull request was built and tested locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants