From 2a0b1644c057a8d5bb0d4da04d143f12810d9631 Mon Sep 17 00:00:00 2001 From: Adam Langley Date: Sat, 25 Oct 2014 11:16:08 -0700 Subject: [PATCH] go.crypto/ssh/terminal: fix crash when terminal narrower than prompt. Previously, if the current line was "empty", resizes wouldn't trigger repaints. However, the line can be empty when the prompt is non-empty and the code would then panic after a resize because the cursor position was outside of the terminal. LGTM=bradfitz R=bradfitz CC=golang-codereviews https://golang.org/cl/158090043 --- terminal.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/terminal.go b/terminal.go index 123de5e..1ee1b44 100644 --- a/terminal.go +++ b/terminal.go @@ -732,11 +732,15 @@ func (t *Terminal) SetSize(width, height int) error { t.lock.Lock() defer t.lock.Unlock() + if width == 0 { + width = 1 + } + oldWidth := t.termWidth t.termWidth, t.termHeight = width, height switch { - case width == oldWidth || len(t.line) == 0: + case width == oldWidth: // If the width didn't change then nothing else needs to be // done. return nil @@ -752,6 +756,9 @@ func (t *Terminal) SetSize(width, height int) error { // wrapping and turning into two. This causes the prompt on // xterms to move upwards, which isn't great, but it avoids a // huge mess with gnome-terminal. + if t.cursorX >= t.termWidth { + t.cursorX = t.termWidth - 1 + } t.cursorY *= 2 t.clearAndRepaintLinePlusNPrevious(t.maxLine * 2) case width > oldWidth: