Skip to content
Myr

Wait for rest of escape sequence before parsing (#4901)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Read loop dropped its escDelay retry budget after every successful byte, so a sequence split across reads reached the parser as a fragment, parsed as ALT-[ with the remainder left behind as query text. - fzf queries DECRQM at startup since dab626b, so a terminal answering late leaked "?2004;2$y" into the query - Same split leaked modified keys and mouse sequences: CTRL-UP left "5A", SGR mouse left "0;1;1M" - Bound unchanged, a stall longer than escDelay still falls back to ALT Fix #4899

authored by Junegunn Choi · committed by GitHub · parent 5cb7bab · Browse files · 2 files+98 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+98 added−0 removed

  • other:+98 added−0 removed

2 files+98 added−0 removed

added:src/tui/light_escape_test.go+53 added
1package tui23import (4 "strings"5 "testing"6)78func TestIncompleteEscape(t *testing.T) {9 for _, c := range []struct {10 buffer string11 want bool12 }{13 // Complete sequences: nothing to wait for14 {"\x1b[A", false},15 {"\x1bOA", false},16 {"\x1b[1;5A", false},17 {"\x1b[200~", false},18 {"\x1b[<0;1;1M", false},19 {"\x1b[12;34R", false},20 {"\x1b[?2004;2$y", false},21 {"\x1b[?1;2c", false},2223 // Fragments: keep waiting24 {"\x1b[", true},25 {"\x1b[?", true},26 {"\x1b[1;", true},27 {"\x1b[?2004;2$", true},28 {"\x1bO", true},29 {"\x1b[<0;1;", true},3031 // Only the trailing sequence matters32 {"ab\x1b[?2004;2$", true},33 {"\x1b[A\x1b[", true},34 {"\x1b[A\x1b[B", false},3536 // Long buffers: only the tail is scanned, so an introducer further37 // back than escapeLookback is not waited for38 {strings.Repeat("a", 100000), false},39 {"\x1b[" + strings.Repeat("a", 100000), false},40 {strings.Repeat("a", 100000) + "\x1b[1;", true},4142 // Not a sequence fzf waits on43 {"", false},44 {"abc", false},45 {"\x1b", false}, // lone ESC, handled by the existing escDelay branch46 {"\x1ba", false}, // ALT-a47 {"\x1b[\x01", false}, // malformed, do not stall on it48 } {49 if got := incompleteEscape([]byte(c.buffer)); got != c.want {50 t.Errorf("incompleteEscape(%q) = %v, want %v", c.buffer, got, c.want)51 }52 }53}
modified:src/tui/light.go+45 added
24 unmodified lines
25 escPollInterval = 526 offsetPollTries = 1027 queryTimeout = 500 * time.Millisecond28 maxInputBuffer = 1024 * 102429 escapeLookback = 25630 maxSelectTries = 10031)3233const DefaultTtyDevice string = "/dev/tty"
304 unmodified lines
338 }339 return atoi(env, defaultValue)340}341342// Bytes of a CSI sequence: parameter and intermediate bytes continue it, a343// final byte ends it. Order is not enforced. Strictness would only make fzf344// give up on a sequence it could have framed.345//346// https://vt100.net/emu/dec_ansi_parser347func csiContinues(b byte) bool { return b >= 0x20 && b <= 0x3f }348func csiFinal(b byte) bool { return b >= 0x40 && b <= 0x7e }349350// incompleteEscape reports whether the buffer ends in an escape sequence that351// has not been terminated yet. The read loop keeps waiting in that case, so the352// parser is never handed a fragment to guess at.353func incompleteEscape(buffer []byte) bool {354 // Only the tail can hold a sequence still arriving. This runs once per byte355 // read, so scanning all of a large paste would make the read quadratic.356 tail := buffer357 if len(tail) > escapeLookback {358 tail = tail[len(tail)-escapeLookback:]359 }360 start := bytes.LastIndexByte(tail, Esc.Byte())361 if start < 0 || len(tail)-start < 2 {362 return false363 }364 switch tail[start+1] {365 case '[':366 for _, b := range tail[start+2:] {367 if csiFinal(b) {368 return false369 }370 if !csiContinues(b) {371 return false // malformed, do not wait for a terminator372 }373 }374 return true375 case 'O':376 return len(tail)-start < 3377 }378 return false379}380381func (r *LightRenderer) getBytes(cancellable bool) ([]byte, getCharResult, error) {382 return r.getBytesInternal(cancellable, r.buffer, false)383}384
32 unmodified lines
417 } else {418 retries = 0419 }420 buffer = append(buffer, byte(c))421 // Keep waiting while a sequence is still arriving. Dropping the budget422 // after every byte left fzf parsing whatever the read happened to end on.423 if retries == 0 && incompleteEscape(buffer) {424 retries = r.escDelay / escPollInterval425 }426 pc = c427428 // This should never happen under normal conditions,429 // so terminate fzf immediately.
1262 unmodified lines