Skip to content

fix: flush unrecognized CSI tilde sequences instead of buffering indefinitely#232

Merged
Aaronontheweb merged 3 commits into
Aaronontheweb:devfrom
jsantos98:fix/esc-parser-csi-tilde-sequences
May 29, 2026
Merged

fix: flush unrecognized CSI tilde sequences instead of buffering indefinitely#232
Aaronontheweb merged 3 commits into
Aaronontheweb:devfrom
jsantos98:fix/esc-parser-csi-tilde-sequences

Conversation

@jsantos98

@jsantos98 jsantos98 commented May 26, 2026

Copy link
Copy Markdown
Contributor

Problem

EscapeSequenceParser.CouldLeadToRecognizedSequence incorrectly returns true for complete but unrecognized CSI tilde-terminated sequences like [5~ (PgUp), [6~ (PgDn), [3~ (Delete), [1~ (Home), [4~ (End).

The second character (5) is a digit, so the CSI-u digit/semicolon/colon check at line 366 returns true — the parser stays stuck in InBracketSequence, silently swallowing all subsequent input (including mouse wheel events).

CheckEscapeTimeout only flushes the AfterEscape state, not InBracketSequence. The parser escapes the stuck state only after 32+ buffered characters are accumulated, since every subsequent byte also matches the digit check.

Fix

Adds a guard before the CSI-u digit check: if the sequence is a complete CSI form ending with ~ and has length >= 3, it cannot become any recognized pattern — return false to flush the sequence as raw KeyPressed events so applications can process them.

Testing

  • 1138/1138 existing Termina unit tests pass
  • Verified in downstream TUI application: PgUp/PgDn scroll output, mouse wheel scrolls output, Up/Down navigates command history

jsantos98 and others added 2 commits May 26, 2026 03:28
…finitely

>
> CouldLeadToRecognizedSequence incorrectly returned true for sequences
> like [5~ (PgUp) and [6~ (PgDn) because seq[1] is a digit and the CSI-u
> length check passes. This caused the parser to stay stuck in
> InBracketSequence, silently swallowing all subsequent input.
>
> The fix: return false for any complete CSI sequence ending with ~
> that starts with [ and has length >= 3. This causes the parser to
> flush the sequence as raw KeyPressed events, which applications can
> then handle via their own input processing.
Covers the reported bug and the safety boundaries of the fix:
- [1~/[3~/[4~/[5~/[6~ flush as raw KeyPressed and unstick the parser
- mouse wheel parses correctly after a [5~ flush (the original symptom)
- modified form [5;3~ also flushes
- bracketed paste still round-trips (guard didn't break paste start/end)

@Aaronontheweb Aaronontheweb left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the full EscapeSequenceParser state machine — the fix is correct and safe. The new seq[^1] == '~' flush sits after the paste-start match (line 184) and paste-end is handled inside PasteBuffering, so bracketed paste is unaffected; no event-producing sequence in this parser terminates in ~, so flushing complete tilde CSIs is correct.

Pushed regression tests (commit b3226b3) covering the gaps:

  • [1~/[3~/[4~/[5~/[6~ flush as raw KeyPressed and leave IsBufferingEscape == false
  • mouse wheel still parses after a [5~ flush (the original swallowed-input symptom)
  • modified [5;3~ also unsticks
  • bracketed paste still round-trips into one PasteEvent

LGTM.

@Aaronontheweb Aaronontheweb left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Aaronontheweb Aaronontheweb disabled auto-merge May 29, 2026 15:34
@Aaronontheweb Aaronontheweb merged commit eb95fd0 into Aaronontheweb:dev May 29, 2026
13 checks passed
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.

2 participants