Browse Source

go.crypto/ssh/terminal: fix non-ASCII history.

The length of history buffer entries (which are stored as strings) was
being used as the number of runes. This was correct until ff9ce887b46b,
which allowed unicode entry, but can now cause a panic when editing
history that contains non-ASCII codepoints.

R=golang-dev, sfrithjof, r
CC=golang-dev
https://golang.org/cl/13255050
Adam Langley 12 years ago
parent
commit
28dc961a18
2 changed files with 16 additions and 3 deletions
  1. 6 3
      ssh/terminal/terminal.go
  2. 10 0
      ssh/terminal/terminal_test.go

+ 6 - 3
ssh/terminal/terminal.go

@@ -409,19 +409,22 @@ func (t *Terminal) handleKey(key rune) (line string, ok bool) {
 			t.historyPending = string(t.line)
 			t.historyPending = string(t.line)
 		}
 		}
 		t.historyIndex++
 		t.historyIndex++
-		t.setLine([]rune(entry), len(entry))
+		runes := []rune(entry)
+		t.setLine(runes, len(runes))
 	case keyDown:
 	case keyDown:
 		switch t.historyIndex {
 		switch t.historyIndex {
 		case -1:
 		case -1:
 			return
 			return
 		case 0:
 		case 0:
-			t.setLine([]rune(t.historyPending), len(t.historyPending))
+			runes := []rune(t.historyPending)
+			t.setLine(runes, len(runes))
 			t.historyIndex--
 			t.historyIndex--
 		default:
 		default:
 			entry, ok := t.history.NthPreviousEntry(t.historyIndex - 1)
 			entry, ok := t.history.NthPreviousEntry(t.historyIndex - 1)
 			if ok {
 			if ok {
 				t.historyIndex--
 				t.historyIndex--
-				t.setLine([]rune(entry), len(entry))
+				runes := []rune(entry)
+				t.setLine(runes, len(runes))
 			}
 			}
 		}
 		}
 	case keyEnter:
 	case keyEnter:

+ 10 - 0
ssh/terminal/terminal_test.go

@@ -141,6 +141,16 @@ var keyPressTests = []struct {
 		in:   "Ξεσκεπάζω\r",
 		in:   "Ξεσκεπάζω\r",
 		line: "Ξεσκεπάζω",
 		line: "Ξεσκεπάζω",
 	},
 	},
+	{
+		in:             "£\r\x1b[A\177\r", // non-ASCII char, enter, up, backspace.
+		line:           "",
+		throwAwayLines: 1,
+	},
+	{
+		in:             "£\r££\x1b[A\x1b[B\177\r", // non-ASCII char, enter, 2x non-ASCII, up, down, backspace, enter.
+		line:           "£",
+		throwAwayLines: 1,
+	},
 }
 }
 
 
 func TestKeyPresses(t *testing.T) {
 func TestKeyPresses(t *testing.T) {