Check escape sequence bytes before counting them (#4929)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
The parser decided how many bytes a key used before checking them, so a key that arrived in the same read was lost or misread. - Recognize F1-F8 and modified Delete, PageUp and PageDown when more input follows them, as with a paste or tmux send-keys. They required the sequence to be the whole buffer. - After \e[ and a digit, do not consume a byte that cannot continue the sequence. ALT-[, 2 and Enter lost the Enter. - Report Home and End for \e[7, \e[4 and \e[8 only before ~, $, ^ or @. ALT-[, 4 and x fired End and lost the x. - Look for the end of a mouse report within its frame. A later M swallowed the keys before it, and repeated \e[< took quadratic time.
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+75 added−14 removed
- other:+75 added−14 removed
Provenance
- Commit
b563e20- Tree
e16abc4- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
c009c10
4 files changed+75 added−14 removed
modified:src/tui/light_csi_test.go+41 added
229 unmodified lines230 t.Errorf("csiEnd(%q) = %d, want %d", c.buffer, got, c.want)231 }232 }233 }234+235+// A key that follows a sequence in the same read must not be consumed with it236+func TestKeyAfterSequence(t *testing.T) {237+ for _, c := range []struct {238+ sequence string239+ mouse bool240+ event EventType241+ size int242+ }{243+ // Recognized when more input follows, as from a paste or tmux send-keys244+ {"\x1b[15~x", false, F5, 5},245+ {"\x1b[3;5~x", false, CtrlDelete, 6},246+ {"\x1b[5;13~x", false, CtrlAltPageUp, 7},247+ {"\x1b[6;2~\x1b[A", false, ShiftPageDown, 6},248+249+ // ALT-[ or ALT-O and a digit, then a key that cannot continue a sequence250+ {"\x1b[2\r", false, Alt, 2},251+ {"\x1b[12\x03", false, Alt, 2},252+ {"\x1bO2\a", false, Alt, 2},253+ {"\x1b[4x", false, Alt, 2}, // not End254+ {"\x1b[7\r", false, Alt, 2}, // not Home255+ {"\x1b[7~x", false, Home, 4},256+ {"\x1b[8^x", false, End, 4}, // rxvt CTRL-END257+258+ // A mouse report ends at its own final byte259+ {"\x1b[<0;5;3Mx", true, Mouse, 9},260+ {"\x1b[<0;1;\x1b[AM", true, Invalid, 3},261+ {"\x1b[<0;1;1xM", true, Invalid, 9},262+ } {263+ r := &LightRenderer{buffer: []byte(c.sequence), mouse: c.mouse}264+ sz := 1265+ event := r.escSequence(&sz)266+ if event.Type != c.event {267+ t.Errorf("escSequence(%q) = %s, want %s",268+ c.sequence, event.Type.String(), c.event.String())269+ }270+ if sz != c.size {271+ t.Errorf("escSequence(%q) consumed %d bytes, want %d", c.sequence, sz, c.size)272+ }273+ }274+}modified:src/tui/light_test.go+1 added-1 removed
64 unmodified lines65 assertEscSequence("\x1b[<", "!Invalid")66 assertEscSequence("\x1b[1;1R", "!Invalid")67 assertEscSequence("\x1b[", "!Invalid")68 assertEscSequence("\x1b[1", "!Invalid")69- assertEscSequence("\x1b[3;3~1", "!Invalid")69 assertEscSequence("\x1b[13", "!Invalid")70 assertEscSequence("\x1b[1;3", "!Invalid")71 assertEscSequence("\x1b[1;10", "!Invalid")72 assertEscSequence("\x1b[220~", "!Invalid")125 unmodified lines198 assertEscSequence("\x1b[1;3D", "alt-left")199 assertEscSequence("\x1b[1;3H", "alt-home")200 assertEscSequence("\x1b[1;3F", "alt-end")201 assertEscSequence("\x1b[3;3~", "alt-delete")202+ assertEscSequence("\x1b[3;3~1", "alt-delete") // then 1203 assertEscSequence("\x1b[5;3~", "alt-page-up")204 assertEscSequence("\x1b[6;3~", "alt-page-down")205 206 assertEscSequence("\x1b[1;4A", "alt-shift-up")274 unmodified linesmodified:src/tui/light.go+32 added-13 removed
398 unmodified lines399 }400 return 0401 }402 403+// rxvtKeyEnd reports whether the byte ends a key such as \e[7~ for Home.404+// rxvt marks modifiers with the final byte: $ for SHIFT, ^ for CTRL, @ for both.405+func rxvtKeyEnd(b byte) bool {406+ return b == '~' || b == '$' || b == '^' || b == '@'407+}408+409 // csiIntroducer reports whether the byte after ESC starts a sequence that is410 // framed by a final byte. The parser routes CSI and SS3 through the same411 // parameterized subcases, so both are framed by csiEnd.412 func csiIntroducer(b byte) bool {339 unmodified lines752 case '1', '2', '3', '4', '5', '6', '7', '8':753 if len(r.buffer) < 4 {754 return Event{Invalid, 0, nil}755 }756+ // A byte that cannot continue the sequence, such as Enter after757+ // ALT-[ and a digit, is a key of its own758+ if csiEnd(r.buffer) < 0 {759+ break760+ }761 *sz = 4762 switch r.buffer[2] {763 case '2':764 if r.buffer[3] == '~' {24 unmodified lines789 case '3':790 if r.buffer[3] == '~' {791 return Event{Delete, 0, nil}792 }782- if len(r.buffer) == 7 && r.buffer[6] == '~' && r.buffer[4] == '1' {793+ if len(r.buffer) > 6 && r.buffer[6] == '~' && r.buffer[4] == '1' {794 *sz = 7795 switch r.buffer[5] {796 case '0':797 return Event{AltShiftDelete, 0, nil}10 unmodified lines808 case '6':809 return Event{CtrlAltShiftDelete, 0, nil}810 }811 }801- if len(r.buffer) == 6 && r.buffer[5] == '~' {812+ if len(r.buffer) > 5 && r.buffer[5] == '~' {813 *sz = 6814 switch r.buffer[4] {815 case '2':816 return Event{ShiftDelete, 0, nil}14 unmodified lines831 }832 }833 return Event{Invalid, 0, nil}834 case '4':835+ if rxvtKeyEnd(r.buffer[3]) {824- return Event{End, 0, nil}836+ return Event{End, 0, nil}837+ }838 case '5':839 if r.buffer[3] == '~' {840 return Event{PageUp, 0, nil}841 }829- if len(r.buffer) == 7 && r.buffer[6] == '~' && r.buffer[4] == '1' {842+ if len(r.buffer) > 6 && r.buffer[6] == '~' && r.buffer[4] == '1' {843 *sz = 7844 switch r.buffer[5] {845 case '0':846 return Event{AltShiftPageUp, 0, nil}10 unmodified lines857 case '6':858 return Event{CtrlAltShiftPageUp, 0, nil}859 }860 }848- if len(r.buffer) == 6 && r.buffer[5] == '~' {861+ if len(r.buffer) > 5 && r.buffer[5] == '~' {862 *sz = 6863 switch r.buffer[4] {864 case '2':865 return Event{ShiftPageUp, 0, nil}17 unmodified lines883 case '6':884 if r.buffer[3] == '~' {885 return Event{PageDown, 0, nil}886 }874- if len(r.buffer) == 7 && r.buffer[6] == '~' && r.buffer[4] == '1' {887+ if len(r.buffer) > 6 && r.buffer[6] == '~' && r.buffer[4] == '1' {888 *sz = 7889 switch r.buffer[5] {890 case '0':891 return Event{AltShiftPageDown, 0, nil}10 unmodified lines902 case '6':903 return Event{CtrlAltShiftPageDown, 0, nil}904 }905 }893- if len(r.buffer) == 6 && r.buffer[5] == '~' {906+ if len(r.buffer) > 5 && r.buffer[5] == '~' {907 *sz = 6908 switch r.buffer[4] {909 case '2':910 return Event{ShiftPageDown, 0, nil}14 unmodified lines925 }926 }927 return Event{Invalid, 0, nil}928 case '7':929+ if rxvtKeyEnd(r.buffer[3]) {916- return Event{Home, 0, nil}930+ return Event{Home, 0, nil}931+ }932 case '8':933+ if rxvtKeyEnd(r.buffer[3]) {918- return Event{End, 0, nil}934+ return Event{End, 0, nil}935+ }936 case '1':937 switch r.buffer[3] {938 case '~':939 return Event{Home, 0, nil}940 case '1', '2', '3', '4', '5', '7', '8', '9':924- if len(r.buffer) == 5 && r.buffer[4] == '~' {941+ if len(r.buffer) > 4 && r.buffer[4] == '~' {942 *sz = 5943 switch r.buffer[3] {944 case '1':945 return Event{F1, 0, nil}232 unmodified lines1178 if len(r.buffer) < 9 || !r.mouse {1179 return Event{Invalid, 0, nil}1180 }1181 1165- rest := r.buffer[*sz:]1166- end := bytes.IndexAny(rest, "mM")1167- if end == -1 {1182+ // Not past the frame, where M or m can be a key typed later1183+ frame := csiEnd(r.buffer)1184+ if frame <= 0 || r.buffer[frame-1] != 'm' && r.buffer[frame-1] != 'M' {1185 return Event{Invalid, 0, nil}1186 }1187+ rest := r.buffer[*sz:]1188+ end := frame - 1 - *sz1189 1190 elems := strings.SplitN(string(rest[:end]), ";", 3)1191 if len(elems) != 3 {1192 return Event{Invalid, 0, nil}683 unmodified linesmodified:CHANGELOG.md+1 added
3 unmodified lines4 0.74.55 ------6 - Fixed `--gap-line` cutting a grapheme cluster when filling the last cells of the line (#4920)7 - Fixed an escape sequence fzf does not recognize being partly consumed, which leaked the rest into the query (#4926)8+- Fixed a key lost when it arrived in the same read as an escape sequence9 10 0.74.411 ------12 - Fixed an escape sequence split across reads being parsed as a fragment, which leaked the rest into the query (#4899)3451 unmodified lines