Repository navigation
Fix #15245 syntaxError on new[] with a braced initializer ending in a parenthesis - #9087
Open
tsobczynski wants to merge 1 commit into
Open
tsobczynski wants to merge 1 commit into
tsobczynski wants to merge 1 commit into
Conversation
… 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
reviewed
Nov 28, 2026
dmcppcheck
left a comment
Contributor
There was a problem hiding this comment.
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
cppcheckandtestrunnerinpr/, andcppcheckinbase/. ./testrunner TestTokenizer TestGarbage TestVarID TestSimplifyTokenspasses, including the newvardecl38. The full./testrunner -qrun 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.
- base gives
- I ran
cppcheck -q --enable=allfrom base and from the PR on these variations and compared the output:{((1))},{(1), (2)},{1,(2)},{f(1)},{(int)1}and{[](){return 1;}()}insidenew 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 bysimplifyVarDecl, as--debug-normalshows.
- 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In
Tokenizer::simplifyVarDecl, the K&R parameter pass no longer steps ontotokEndwhen it skips a parenthesised group, which made the loop run past the end of its range. Test:vardecl38.🤖 Generated with Claude Code