Skip to content

Fix out-of-bounds accesses in the replxx line editor - #126120

Merged
alexey-milovidov merged 6 commits into
ClickHouse:masterfrom
groeneai:fix-replxx-history-search-cursor
Oct 18, 2026
Merged

alexey-milovidov merged 6 commits into
ClickHouse:masterfrom
groeneai:fix-replxx-history-search-cursor

Conversation

@groeneai

@groeneai groeneai commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #126008
Related: ClickHouse/replxx#50

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 crashes of the interactive line editor of clickhouse-client and clickhouse-local, which also crashed the server when used through the web terminal or an SSH session: after a history search that found nothing on an edited recalled query, when the prompt or the line given to an Alt-R history search contained a tab, when leaving a history search with a double-width character in the prompt (for example in the server's display_name), and on Ctrl-W or Alt-. after some non-ASCII characters. Closes #126008.

Description

The web terminal and SSH endpoint run the client's replxx line editor inside the server, so input that crashes the editor kills the server (#126008). Four defects were reachable:

  1. An incremental history search (Ctrl-S, Ctrl-T, Alt-R, and Ctrl-R in the web terminal and SSH sessions) started on a recalled and edited query mapped the cursor onto the old text of the history entry. A search that found nothing left the cursor past the end of the line, and the next edit wrote out of bounds.
  2. Ctrl-W and Alt-. passed code points to isspace, which is undefined above 255 (SIGSEGV on U+10FFFF).
  3. A tab renders as 4 spaces, but the prompt was rendered in place. Alt-R copies the line into the search prompt, so a line with a tab (or a --prompt with one) overflowed the buffer.
  4. The prompt's last line start was computed as characters minus the cursor column, negative for a double-width character (e.g. a display_name of 日本), so leaving a history search read before the prompt text.

The submodule moves to replxx master, where ClickHouse/replxx#50 merged these fixes and ClickHouse/replxx#49 fixes a decode of an uninitialized byte on POLLHUP without POLLIN introduced by the Esc delay support. It also brings ClickHouse/replxx#36 and #43-#48.

Related: ClickHouse/replxx#49

Category. On 26.3.42.3 and 26.8.18.2, clickhouse-local aborts on Ctrl-P, an edit, a Ctrl-S search finding nothing and a key; on Alt-R after pasting a query with a tab; and on leaving a Ctrl-T search with a double-width character in the prompt.

Validation. 05401_client_history_search_crash (a case per defect) passes 50/50, failing on master and without fix 4. Issue's web terminal reproducer: master died within 3-14 attempts, the fix survived 300 with display_name 日本. Random keys in clickhouse-local: master 74 aborts in 15 minutes, the fix 0 in 10. With replxx master: the test 50/50, 04909_client_hints_history_navigation 10/10, the web terminal survived 150 random frames.


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

Version info

  • Merged into: 26.11.1.154-master (included in 26.11 and later)
  • Backported to: 26.10.2.13, 26.9.22.3, 26.8.31.2, 26.7.35.6, 26.3.57.4

Update `replxx` with four fixes:

- An incremental history search (Ctrl-S, Ctrl-T, Meta-R, and Ctrl-R in the
  web terminal and SSH sessions) that started on a recalled and then edited
  history line mapped the cursor of the edited line onto the old text of the
  entry, so a search that found nothing left the cursor past the end of the
  line. The next edit wrote out of bounds and aborted
  `clickhouse-client`/`clickhouse-local`; through the web terminal or an SSH
  PTY session, which run the editor inside the server, it aborted the server.
- Ctrl-W and Meta-. passed `char32_t` code points to `isspace`, which is
  undefined for values above 255 and crashed on e.g. U+10FFFF.
- A tab renders as 4 spaces, but the prompt was rendered in place, so a search
  prompt with a tab (Meta-R on a line with a tab) overflowed its buffer.
- The prompt's last line position was the number of characters minus the
  cursor column, which is negative for a prompt with a double-width character
  (e.g. in the server's `display_name`), so leaving a history search read
  before the prompt text and aborted.

The commits are on top of the current pointer, as the broader `replxx` update
goes into a separate PR.

Closes ClickHouse#126008

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

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

Pre-publication review by an independent model (engine: codex; 3 rounds, 4 findings) plus my own review (3 findings).

# Sev Finding Verdict Evidence / action
1 ⚠️ A prompt with a double-width character (e.g. a CJK display_name) makes the prompt's last line position negative, and leaving a history search reads before the prompt text (prompt.cxx:58, replxx_impl.cxx:2456) AGREE, fixed @ 0bb6256 4th replxx commit; test case 3
2 ⚠️ The test does not exercise the iswspace change (Ctrl-W, Alt-.) AGREE, fixed @ 0bb6256 test case 4, U+10FFFF sent as raw UTF-8
3 ⚠️ Test case 4's Ctrl-W half goes red without the fix only through a SIGSEGV, since a trailing code point is killed either way AGREE, fixed @ 0bb6256 the code point now sits inside the killed word. A fully deterministic check is not possible: when the out-of-range isspace read happens to return "not a space", the old code behaves exactly like the fix
4 💡 Value checks 127 / 42 can match inside the printed query id AGREE, fixed @ 0bb6256 anchored as whitespace-delimited tokens
5 💡 Ctrl-R also reaches the search in the web terminal and SSH sessions (no skim there) AGREE, fixed @ 0bb6256 commit message and description
6 💡 Use Critical Bug Fix because the change fixes crashes DISAGREE Critical Bug Fix is used only when a maintainer asks for it; the Bug Fix test holds (26.3 / 26.8 abort on the key sequences)
7 💡 The submodule commit is not published yet DISAGREE publication order: the replxx branches are pushed and ClickHouse/replxx#51 is verified to point at the commit before this PR is opened

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

Session id: cron:clickhouse-review-slot-48:20261015-200600

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. 05401_client_history_search_crash aborts the client 3/3 on master (case 1: Ctrl-P, 23, Ctrl-S, ", Ctrl-F, +4), 3/3 with fixes 1-3 only (case 3: --prompt '日 :) ', Ctrl-T, x, Ctrl-G), and case 4 alone (Ctrl-W / Alt-. with U+10FFFF inside a word) fails 23/30 on master: it reads outside the isspace table, so the result depends on the memory layout. The same keys with a 24-column prompt in a 24-column terminal abort 3/3 without fix 4.
b Root cause explained? (1) A search on a recalled and edited entry mapped _pos onto the entry's old text, so a failed search left _pos past the end of the line. (2) isspace on char32_t is undefined above 255. (3) Prompt::update_state rendered in place while a tab renders as 4 characters. (4) The last line start of the prompt was visible characters minus the cursor column, negative with a double-width character and equal to the length when the last row is empty.
c Fix matches root cause? Yes: the edited line is stored as the entry's scratch before the search; iswspace; the prompt is rendered into a separate buffer; virtual_render records where the last rendered line starts and the search exit copies from there with the substring constructor. No guards at the crash sites.
d Test intent preserved / new tests added? New 05401_client_history_search_crash.expect, one case per defect; value checks anchored as whitespace-delimited tokens.
e Both directions demonstrated? Fix: 50/50 OK. Master: 3/3 FAIL (case 1). Fixes 1-3 only: 3/3 FAIL at case 3 after cases 1-2 passed. Case 4 alone: master 23/30 FAIL (22 SIGSEGV, 1 wrong Ctrl-W kill), fix 10/10 OK. Web terminal with display_name 日本: fixes 1-3 server dies at the first Ctrl-T, x, Ctrl-G; the fix survives it 5 times and 300 random inputs.
f Fix is general across code paths? Prompt::update_state is the only caller of virtual_render with an output buffer, and every prompt text goes through it; the search exit is the only index use of _lastLinePosition; indentation() still returns the cursor column. Measured: Alt-R seeded, recalled previous search text, --prompt with a tab, with a wide character, and filling the terminal width.
g Fix generalizes across inputs (params/datatypes/wrappers)? Tabs from Ctrl-V, bracketed paste and --prompt; double-width prompt characters; a prompt that exactly fills the terminal width; U+10FFFF in Ctrl-W / Alt-.; empty search text. 10 minutes of random keys with a 日本 :) prompt and 300 random web terminal inputs: 0 crashes.
h Backward compatible? (maintainer-approved exception only) Yes. No settings or formats; rendering is unchanged; for a prompt of printable one-column characters the last line start is the same as before.
i Invariants and contracts preserved? _lastLinePosition is now always in [0, _text.length()]; indentation() is the cursor column as before; virtual_render without a buffer behaves as before. The edited line is kept as the history scratch, as history_move already does.

Session id: cron:clickhouse-impl-slot-7:20261015-195200

@clickhouse-gh

clickhouse-gh Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [80ee386]

Summary: ✅


AI Review

Summary

Updates replxx to fix out-of-bounds accesses in history search, prompt rendering, and Unicode word editing, with focused regression coverage. No actionable findings remain; the previously reported decoder hangup issue is fixed in the pinned revision. The new regression passed the current head's Fast test, and seven standalone checks against the pinned dependency passed under ASan/UBSan.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default submodule changed At least one submodule changed in this PR. labels Oct 15, 2026
@alexey-milovidov alexey-milovidov added can be tested Allows running workflows for external contributors pr-must-backport Pull request should be backported intentionally. Use this label with great care! labels Oct 15, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-external-dependencies Third-party deps updates in contrib/, vendored code, and platform base libraries. label Oct 15, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

Merged. Update the submodule pointer to master.

@alexey-milovidov alexey-milovidov self-assigned this Oct 15, 2026
ClickHouse/replxx#50 is merged into `master` of `replxx`, and the copy of
the same fixes on top of the previous pointer (ClickHouse/replxx#51) is
closed. Point the submodule to the merge commit.

This also brings the `master` changes that were not in the previous
pointer: editing modes, `Esc` delay, prompt indentation, per-terminal
state and the Meta+Up fix (ClickHouse/replxx#36, ClickHouse#43, ClickHouse#44, ClickHouse#45, ClickHouse#46, ClickHouse#47,
ClickHouse#48). `04909_client_hints_history_navigation`, which the `Esc` delay
broke before ClickHouse#48, passes 10/10 locally.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added the manual approve Manual approve required to run CI label Oct 15, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

Done in 6491afb: contrib/replxx now points to replxx master (950479695482). 05401_client_history_search_crash passes 50/50 and 04909_client_hints_history_navigation 10/10 with it.

replxx master does not have ClickHouse/replxx#49 yet: since the Esc delay support (ClickHouse/replxx#36), read_unicode_character decodes an uninitialized byte when poll reports a hangup without POLLIN. I could not make it visible from the web terminal or a closed PTY, but once #49 is merged I can move the pointer again.

Comment thread contrib/replxx Resolved
alexey-milovidov and others added 2 commits October 16, 2026 01:27
Moving `contrib/replxx` to `replxx` master brought in a regression from the
`Esc` delay support: `read_unicode_character` called `read` only when `poll`
reported `POLLIN`, so on `POLLHUP`, `POLLERR`, or `POLLNVAL` alone the
uninitialized character was decoded as input. The pointer now goes to the
merge of `replxx` master into the branch of ClickHouse/replxx#49, which calls
`read` on any event (end of input or error ends the input, as before), and
also re-reads the terminal width on refresh for embedded clients that do not
receive `SIGWINCH`.

Addresses review: ClickHouse#126120 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ClickHouse/replxx#49 is merged into `master` of `replxx`. Point the
submodule to the merge commit; its tree is the same as the head of the
ClickHouse#49 branch used before, so the code does not change.

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

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 4dbd411

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
Performance Comparison (arm_release, master_head, 3/6, 4/6) job timeout (7200 s), not from this PR: one of the shard's two Check CI Log cluster probes (setup_log_cluster.sh) blocked about 3645 s on the overloaded CI Logs cluster before any perf query ran, so the job was killed mid-run with no verdicts. Same on dozens of PRs and master between 01:40Z and 05:30Z #126217 (mine, open)
Stateless tests (amd_debug, parallel) / 05394_filesystem_cache_slru_promote_partial_segment flaky test, not from this PR (it only touches replxx history search); the build predates the test fix #126174 (merged)
Mergeable Check, PR praktika rollups of the rows above #126217 and #126174 (the owners of the rows above)

Session id: cron:our-pr-ci-monitor:20261016-070011

@clickhouse-gh

clickhouse-gh Bot commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 80ee3865d with master 0a3052562 (stripped binary size, per-symbol sizes and ThinLTO time; 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

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

Job report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 18, 2026
Merged via the queue into ClickHouse:master with commit 46e9317 Oct 18, 2026
178 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 18, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 18, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 19, 2026
Backport #126120 to 26.10: Fix out-of-bounds accesses in the replxx line editor
clickhouse-gh Bot added a commit that referenced this pull request Oct 19, 2026
Backport #126120 to 26.8: Fix out-of-bounds accesses in the replxx line editor
clickhouse-gh Bot added a commit that referenced this pull request Oct 19, 2026
Backport #126120 to 26.9: Fix out-of-bounds accesses in the replxx line editor
PedroTadim added a commit that referenced this pull request Oct 19, 2026
Cherry-pick of #126120 onto 26.3. The fix moved contrib/replxx to the replxx master, which also
brings unrelated features (editing modes, actions, ...). Here contrib/replxx points to
bcab1537d03d (https://github.com/ClickHouse/replxx/tree/clickhouse/26.3-out-of-bounds-fixes): this branch's replxx with only the fixes
cherry-picked, i.e. ClickHouse/replxx#50 and the terminal width re-read
from ClickHouse/replxx#49 (its POLLHUP fix is for the Esc delay code that
is not on this branch). The changelog and version_date.tsv lines that came along from a master
merge on the PR branch are left out.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
PedroTadim added a commit that referenced this pull request Oct 19, 2026
Cherry-pick of #126120 onto 26.7. The fix moved contrib/replxx to the replxx master, which also
brings unrelated features (editing modes, actions, ...). Here contrib/replxx points to
93cbc49d3fe1 (https://github.com/ClickHouse/replxx/tree/clickhouse/26.7-out-of-bounds-fixes): this branch's replxx with only the fixes
cherry-picked, i.e. ClickHouse/replxx#50 and the terminal width re-read
from ClickHouse/replxx#49 (its POLLHUP fix is for the Esc delay code that
is not on this branch). The changelog and version_date.tsv lines that came along from a master
merge on the PR branch are left out.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 19, 2026
alexey-milovidov added a commit that referenced this pull request Oct 19, 2026
Backport #126120 to 26.3: Fix out-of-bounds accesses in the replxx line editor
clickhouse-gh Bot added a commit that referenced this pull request Oct 19, 2026
Backport #126120 to 26.7: Fix out-of-bounds accesses in the replxx line editor
strtgbb pushed a commit to Altinity/ClickHouse that referenced this pull request Oct 21, 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-external-dependencies Third-party deps updates in contrib/, vendored code, and platform base libraries. manual approve Manual approve required to run CI 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 submodule changed At least one submodule changed in this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Web terminal: arbitrary PTY input crashes the whole server (replxx cursor/buffer desync)

5 participants