Repository navigation
Fix out-of-bounds write in query highlighting on malformed UTF-8 - #127906
alexey-milovidov merged 2 commits into
Conversation
The highlighter partitions the text into code points by stepping
`UTF8::seqLength` bytes, and sizes `colors` by the same walk. The AST
formatter writes string-literal bytes above 0x7F raw, so
`SELECT length(x'E2')` is echoed as `SELECT length('<E2>')`, and the
3-byte lead `E2` swallows the closing `')`. The `)` then maps to
`colors.size()`, and the unguarded bright-bracket write in `highlight()`
(always taken from `highlighted()`, which passes cursor 0) indexes past
the end: a libc++ hardening abort, or an out-of-bounds write without
hardening. Through the web terminal this aborts the server (Stress test,
`02426_pod_array_overflow_3`). The same walk made the render loop in
`highlighted()` read past the end of the string (`SELECT x'F0'`) and,
when text follows the bracket, put the bracket color on the next
character.
Fix the partition itself instead of guarding the write sites: a lead byte
spans only the continuation bytes that follow it, capped by its declared
length and by the end of the text. Every walk in the file uses this one
helper, so every position the highlighter indexes is a code-point start
before the end and all `colors` writes are in bounds. For valid UTF-8 the
partition equals the `seqLength` one, so the replxx callback (which always
passes valid UTF-8) highlights exactly as before.
Clamping `seqLength` to the remaining bytes would not be enough: `E2 ' )`
fits exactly in 3 bytes.
Closes ClickHouse#127899
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the codePointLength comment to one line. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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 of the same head (0 findings).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261027-015800 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-40:20261027-013700 |
|
cc @azat @alexey-milovidov, could you review this and add the |
|
Workflow [PR], commit [9e79841] Summary: ❌
AI ReviewSummaryPR #127906 fixes out-of-bounds access when highlighting malformed UTF-8 by using a bounded, consistent byte partition for color allocation, position mapping, and rendering. Reviewed head Final VerdictStatus: ✅ Approve |
CI finish ledger - 9e79841Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
The first run of this head stopped at Session id: cron:our-pr-ci-monitor:20261027-203012 |
14041ab
Closes: #127899
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed an abort of
clickhouse-client,clickhouse-localandclickhouse-format --hilitewhen highlighting a query whose string literal ends with an incomplete UTF-8 sequence, for exampleSELECT length(x'E2'). Such a query is now highlighted without reading past its end, and the closing bracket gets its own color.Description
On 26.3.42.3,
clickhouse format --hilite --query "SELECT length(x'E2')"aborts withlibc++ Hardening assertion __n < size() failed: vector[] index out of bounds, and the interactiveclickhouse-clientaborts the same way when it echoes that query. On master the same abort takes down the server inStress test (amd_tsan)(report), where02426_pod_array_overflow_3runs through the web terminal.The highlighter maps bytes to entries of
colors(one per code point) by steppingUTF8::seqLengthbytes. The formatter writes string-literal bytes above 0x7F raw, so the query is echoed asSELECT length('<E2>'), and the 3-byte leadE2swallows the closing'). The)then maps tocolors.size(), and the unguarded bracket write indexes past the end. The same walk makes the render loop read past the end of the string (SELECT x'F0') and, when text follows the bracket, puts the bracket color on the next character.The fix changes the partition itself: a lead byte now spans only the continuation bytes that actually follow it, capped by the end of the text. Every walk in the file uses that one helper, so every index stays in bounds. For valid UTF-8 the partition, and so the highlighted output, is unchanged.
Validation: the new test fails on master (four aborts, plus the over-read) and passes with the fix, 50 runs. The interactive client and the CI query from
02426_pod_array_overflow_3behave the same way. The existing highlighting tests pass.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/127906]
Version info
26.11.1.885-master(included in26.11and later)