From ab3007f75cff15b25afa377d4c7937555539eac1 Mon Sep 17 00:00:00 2001 From: Tom Sobczynski Date: Thu, 19 Nov 2026 16:26:53 -0500 Subject: [PATCH] Fix #15245 syntaxError on new[] with a braced initializer ending in a 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 --- lib/tokenize.cpp | 3 ++- test/testtokenize.cpp | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index a13b05cef62..6018d264376 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7597,7 +7597,8 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, const Token * const tokEnd, co if (only_k_r_fpar && finishedwithkr) { if (Token::Match(tok, "(|[|{")) { tok = tok->link(); - if (tok->next() && Token::Match(tok, ") !!{")) + // Don't step onto tokEnd: the loop increment would then step past it + if (tok->next() && tok->next() != tokEnd && Token::Match(tok, ") !!{")) tok = tok->next(); else continue; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index d527b98f62c..ec3bdda6e00 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -241,6 +241,7 @@ class TestTokenizer : public TestFixture { TEST_CASE(vardecl34); TEST_CASE(vardecl35); TEST_CASE(vardecl36); + TEST_CASE(vardecl38); TEST_CASE(vardecl_stl_1); TEST_CASE(vardecl_stl_2); TEST_CASE(vardecl_stl_3); @@ -3081,6 +3082,11 @@ class TestTokenizer : public TestFixture { ASSERT_EQUALS(expected, tokenizeAndStringify(code)); } + void vardecl38() { // #15245 range ending in ')' before a braced initializer's '}' + ASSERT_EQUALS("int * q { new int [ 2 ] { ( 1 ) } } ;", + tokenizeAndStringify("int* q{new int[2]{(1)}};")); + } + void volatile_variables() { { const char code[] = "volatile int a=0;\n"