Skip to content
Myr

Fix panic on a read error during the second-chance read

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

The read error left a nil buffer, and the deferred slice in GetChar panicked instead of returning Fatal.

authored by Junegunn Choi · committed by Junegunn Choi · parent b563e20 · Browse files · 2 files+28 added−1 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+28 added−1 removed

  • other:+28 added−1 removed

2 files+28 added−1 removed

modified:src/tui/light_query_test.go+25 added
5 unmodified lines
6 "os"7 "strings"8 "testing"9 "time"1011 "golang.org/x/term"12)1314// Drives queryStartup against a terminal simulated by pipes, with the replies15// already queued so the exchange is deterministic.
140 unmodified lines
156 }157 }158 t.Error("the late key was dropped")159}160161// A read error during the second-chance read must end fzf cleanly, not panic.162func TestSecondChanceReadError(t *testing.T) {163 inR, inW, err := os.Pipe()164 if err != nil {165 t.Fatal(err)166 }167 t.Cleanup(func() { inR.Close() })168 inW.WriteString("\x1b[12")169 go func() {170 time.Sleep(2 * defaultEscDelay * time.Millisecond)171 inW.Close()172 }()173174 out, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0)175 if err != nil {176 t.Fatal(err)177 }178 r := &LightRenderer{ttyin: inR, ttyout: out, escDelay: defaultEscDelay, origState: &term.State{}}179 if ev := r.GetChar(false); ev.Type != Fatal {180 t.Errorf("got %s, want Fatal", ev.Type.String())181 }182}
modified:src/tui/light.go+3 added-1 removed
591 unmodified lines
592 // otherwise blocks until the next keystroke, holding back whatever593 // follows in the buffer.594 if ev.Type == Invalid && stillArriving(r.buffer) {595 waited := len(r.buffer)596 var buffer []byte596 r.buffer, result, err = r.getBytes(true)597 buffer, result, err = r.getBytes(true)598 if err != nil {599 return Event{Fatal, 0, nil}600 }601 r.buffer = buffer602 if result == getCharCancelled {603 return Event{Invalid, 0, nil}604 }605
1272 unmodified lines