Skip to content

Clamp line count in SU, SD, IL and DL to the scroll region - #6318

Open
breeezly1228 wants to merge 1 commit into
xtermjs:masterfrom
breeezly1228:fix/clamp-scroll-line-count
Open

breeezly1228 wants to merge 1 commit into
xtermjs:masterfrom
breeezly1228:fix/clamp-scroll-line-count

Conversation

@breeezly1228

Copy link
Copy Markdown

Fixes #6317

What

Clamps the line count parameter (param) in src/common/InputHandler.ts:

  • scrollUp (CSI Ps S) and scrollDown (CSI Ps T, CSI Ps ^):
    param = Math.min(param, this._activeBuffer.scrollBottom - this._activeBuffer.scrollTop + 1);
  • insertLines (CSI Ps L) and deleteLines (CSI Ps M):
    param = Math.min(param, this._activeBuffer.scrollBottom - this._activeBuffer.y + 1);

Why

EscapeSequenceParser allows CSI numeric parameters up to MAX_INT (2147483647). Previously, scrollUp, scrollDown, insertLines, and deleteLines executed while (param--) directly with the unclamped parameter. Passing a large parameter (e.g. \x1b[22936629T or \x1b[2147483647T) caused tens of millions to billions of CircularList.splice and Buffer.getBlankLine calls, blocking the main thread for tens of seconds to tens of minutes.

Because all four methods operate in-place within the active scroll region ([scrollTop, scrollBottom] for SU/SD and [y, scrollBottom] for IL/DL), clamping param to the number of lines in the affected range produces the same final buffer, scrollback, cursor, and marker state while bounding loop iterations to at most rows.

Testing

  • Added SU, SD, IL and DL regression tests under describe('scroll margins', ...) in src/common/InputHandler.test.ts. Each test sends Ps = 2147483647, checks the final buffer contents, and wraps buffer.lines.splice so that more line operations than the affected range allows fail the test immediately.
  • Built with npm run build && npm run esbuild; all 198 tests in out-esbuild/common/InputHandler.test.js pass.
  • Removing the four clamps makes every new test fail with a clear operation-limit message (expected at most 6 line operations, got 7) in 0–5 ms per test, instead of running billions of iterations. Restored the clamps, rebuilt, and reran all 198 tests successfully.
  • Ran differential tests comparing unpatched and patched builds across randomized viewport sizes, scrollback sizes, DECSTBM scroll regions, DECOM origin mode, wrapped lines, SGR erase attributes, cursor positions and line markers: 10,000 trials on an InputHandler harness and 2,000 trials on full headless Terminal instances via term.write(...), with 0 mismatches.
  • Both changed files pass ESLint and the repository's oxlint checks.

Disclosure: The investigation, patch and tests were prepared with AI assistance (Google Gemini, OpenAI Codex, Anthropic Claude). I reviewed the write-up before submitting.

🤖 Generated with Claude Code

scrollUp, scrollDown, insertLines and deleteLines looped once per
requested line with no upper bound, so a CSI parameter up to
2147483647 (e.g. ESC[22936629T, 11 bytes) blocked the main thread for
tens of seconds to tens of minutes. Repeating past the number of lines
in the affected range does not change the buffer, so clamp the count to
that range. Add regression tests that fail immediately if the clamp is
removed.

Fixes xtermjs#6317

Prepared with AI assistance (Google Gemini, OpenAI Codex, Anthropic
Claude).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unclamped repeat count in SD, SU, IL, and DL (CSI T/S/L/M) causes main-thread hang on large parameter

1 participant