Skip to content

Stop the ARRAY[ constructor lookahead at the end of the statement - #128475

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:array-constructor-lookahead-stops-at-semicolon
Nov 3, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:array-constructor-lookahead-stops-at-semicolon

Conversation

@groeneai

@groeneai groeneai commented Nov 1, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #124763

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed a syntax error message that contained bytes read past the end of the query text when an ARRAY[ was not closed before the ;, for example SELECT ARRAY[f(1); 2]. The error points at the ; again.

Description

Found by the nightly select_parser_fuzzer (ASan heap-buffer-overflow in UTF8::computeBytesBeforeWidth from writeQueryAroundTheError): https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=c189bd99fbda59c78489a6734c49397533bdd591&name_0=NightlyFuzzers&name_1=libFuzzer%20tests.

On 26.10.2.103-stable, SELECT ARRAY[f(1); 2] sent over HTTP returns Syntax error: failed at position 21 (]): ] followed by bytes read past the end of the query text; 26.9.25.15 returns failed at position 18 (end of query): ;..

isArrayConstructorWithKeyword (#124763) scans the tokens after ARRAY[ for a depth-0 comma or closing bracket and does not stop at ;, so it reads into the next statement. The tokens it looks at move Tokens::max(), which tryParseQuery reports as the error position, while the excerpt ends at the first statement's ;. writeQueryAroundTheError then computes end - begin with begin > end, and the unsigned length wraps.

A ; now ends the scan with false. A ; token cannot be inside the brackets of one statement, so such an ARRAY[ is not an array constructor, and the parser fails at the ; as before #124763. No valid query changes meaning. Within one statement the scan still reads to the end of the statement (SELECT ARRAY[f(1) FROM t reports the end of the query), which stays inside the text the message shows.

Validation: the ASan build of master aborts on the fuzzer input, SELECT ARRAY[f(1); 2] and SELECT ARRAY[f(1); SELECT 1, 2]; with this change (debug and ASan builds) all three return SYNTAX_ERROR at the ; without an ASan report. The new test fails on master and with the new case removed, and passes 50/50 randomized runs; the syntax-error and Trino dialect tests pass unchanged.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/128475]

Version info

  • Merged into: 26.11.1.1159-master (included in 26.11 and later)

isArrayConstructorWithKeyword (added in ClickHouse#124763) decides whether `ARRAY[`
starts the array constructor by scanning forward for a depth-0 comma or
closing bracket. The scan did not stop at `;`, so for an `ARRAY[` that is
not closed in its own statement it read tokens of the next statement.
Tokens::max() follows every token a parser looks at and is what
tryParseQuery reports as the error position, while the generic error
branch passes the end of the first statement as the end of the excerpt.
writeQueryAroundTheError then computed `end - begin` with begin > end, and
the unsigned length wrapped: the message contained bytes read past the end
of the query text, and ASan builds aborted (found by select_parser_fuzzer,
heap-buffer-overflow in UTF8::computeBytesBeforeWidth).

A `;` token cannot be inside the brackets of one statement, so the scan now
returns false there: such an `ARRAY[` is not an array constructor, and the
parser fails at the `;` exactly as before ClickHouse#124763. Valid queries are not
affected.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

groeneai commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 0 findings) plus my own cold review (1 nit).

# Sev Finding Verdict Evidence / action
1 💡 The generic and excessive-input error branches of tryParseQuery do not clamp end with last_token.end as the lexical and unmatched-bracket branches do (src/Parsers/parseQuery.cpp) DISAGREE After this change no parser reads past the statement's ; (census of the token scans in src/Parsers), so the clamp would guard an unreachable state and would still report a token of the next statement; one layer at the source.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-8:20261101-115900

@groeneai

groeneai commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. clickhouse local --query "SELECT ARRAY[f(1); 2]" on the master ASan build aborts every time (READ of size 32 in computeBytesBeforeWidth from writeQueryAroundTheError); the fuzzer input reproduces the CI heap-buffer-overflow.
b Root cause explained? isArrayConstructorWithKeyword scans past the statement's ;, so Tokens::max() (the reported error position) lands in the next statement while the excerpt ends at the first ;; end - begin wraps in writeQueryAroundTheError.
c Fix matches root cause? Yes. The scan stops at ;, which restores the formatter's precondition; no clamp in the formatter.
d Test intent preserved / new tests added? New test 05479_array_constructor_lookahead_stops_at_statement_end covers both outcomes of the scan (closing bracket and comma in the next statement). No existing test changed.
e Both directions demonstrated? Yes. The new test fails on master (debug: wrong position plus garbage; ASan: abort) and with the new case removed, passes with the fix; 50/50 randomized runs pass.
f Fix is general across code paths? I checked the other token scans reachable from the query parsers; only this one reads past a ;. formatQuery and multi-statement input go through the same path and are fixed too.
g Fix generalizes across inputs (params/datatypes/wrappers)? Token-level change, no types involved. Probed ; at depth 0 and inside nested brackets, empty ARRAY[], one and several elements, unclosed ARRAY[ without ;, and the Trino dialect: all memory-safe, valid queries unchanged.
h Backward compatible? (maintainer-approved exception only) Yes. Only statements in which the scan reaches a ; change, and all of them are invalid; no setting or format change.
i Invariants and contracts preserved? While a statement is parsed, no token after its ; is read, so every error branch of tryParseQuery again formats a position inside the statement it shows.

Session id: cron:clickhouse-impl-slot-7:20261101-110900

@groeneai

groeneai commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator Author

cc @alexey-milovidov @george-larionov, could you review this and add the can be tested label so CI runs? The ARRAY[ lookahead from #124763 scanned past the statement's ;, so the syntax error position landed in the next statement and writeQueryAroundTheError read past the end of the query text (found by select_parser_fuzzer); the scan now stops at ;.

@clickhouse-gh

clickhouse-gh Bot commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [678f9e3]

Summary: ✅


AI Review

Summary

The PR stops ARRAY[ constructor lookahead at the first ;, preventing out-of-bounds reads when formatting syntax errors for malformed multi-statement input. No actionable findings.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Nov 1, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Nov 1, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-sql-syntax SQL/grammar parsing, AST nodes, syntax-level features. label Nov 1, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 678f9e3a4 with master a0b5e3cec (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 20409c648; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 9 s compile time in total, 7 of them have a recent master baseline.

Job report

@groeneai

groeneai commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 94e53ad

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Stateless tests (arm_binary, AsyncInsert, s3 storage, parallel) / 04653_packed_part_size_accounting_no_read_in_commit flaky, not from this PR (it changes only the ARRAY[ lookahead of the parser): fewer_file_open printed 0 instead of 1, and 74 of 74 reruns with the same settings passed; true master and 18 other PRs in 30 days #118234 (ours, open)
Mergeable Check, PR praktika rollups of the row above #118234 (ours, open)

CIDB has no other FAIL or ERROR row on this head.

Session id: cron:our-pr-ci-monitor:20261101-163005

@alexey-milovidov alexey-milovidov added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Nov 1, 2026
@alexey-milovidov alexey-milovidov self-assigned this Nov 1, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 2, 2026
Merged via the queue into ClickHouse:master with commit 230ae8e Nov 3, 2026
179 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 3, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 3, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 3, 2026
Backport #128475 to 26.10: Stop the `ARRAY[` constructor lookahead at the end of the statement
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 3, 2026
leshikus pushed a commit that referenced this pull request Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-sql-syntax SQL/grammar parsing, AST nodes, syntax-level features. pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants