From b28307baeb6fcfa330a2670cbec12a6f2967a4d9 Mon Sep 17 00:00:00 2001 From: Chris Waldon Date: Wed, 9 Feb 2022 10:08:05 -0500 Subject: [PATCH] widget: ensure buffer.Read does nothing to read zero bytes This commit ensures that the edit buffer used by widget.Editor does not get EOF when trying to read zero bytes from the underlying buffer, which eliminates a panic when calling Editor.SelectedText(). Signed-off-by: Chris Waldon --- widget/buffer.go | 3 +++ widget/editor_test.go | 3 +++ 2 files changed, 6 insertions(+) diff --git a/widget/buffer.go b/widget/buffer.go index b6e89c85..40e02a86 100644 --- a/widget/buffer.go +++ b/widget/buffer.go @@ -113,6 +113,9 @@ func (e *editBuffer) Seek(offset int64, whence int) (ret int64, err error) { } func (e *editBuffer) Read(p []byte) (int, error) { + if len(p) == 0 { + return 0, nil + } if e.pos == e.len() { return 0, io.EOF } diff --git a/widget/editor_test.go b/widget/editor_test.go index 18c00732..4e14de75 100644 --- a/widget/editor_test.go +++ b/widget/editor_test.go @@ -121,6 +121,9 @@ func assertCaret(t *testing.T, e *Editor, line, col, bytes int) { if bytes != caretBytes { t.Errorf("caret at buffer position %d, expected %d", caretBytes, bytes) } + // Ensure that SelectedText() does not panic no matter what the + // editor's state is. + _ = e.SelectedText() } type editMutation int