From e553da2c61cd28fecc684726a35648ad1faf35e6 Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Wed, 4 May 2022 09:49:22 +0200 Subject: [PATCH] fixed panic when writing to zero-sized buffer --- buffer.go | 4 +++- buffer_test.go | 5 +++++ go.sum | 0 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 go.sum diff --git a/buffer.go b/buffer.go index 5c62467..eec10ed 100644 --- a/buffer.go +++ b/buffer.go @@ -42,7 +42,9 @@ func (b *Buffer[T]) y(y int) int { // Set sets the value at position (x,y) to c func (b *Buffer[T]) Set(x, y int, v T) { - b.data[b.y(y)][b.x(x)] = v + if b.width >= 0 && b.height >= 0 { + b.data[b.y(y)][b.x(x)] = v + } } // Get returns the value at position (x,y) diff --git a/buffer_test.go b/buffer_test.go index 2802ae2..64e55cf 100644 --- a/buffer_test.go +++ b/buffer_test.go @@ -17,3 +17,8 @@ func TestSub(t *testing.T) { fmt.Println(strings.Repeat("-", 10)) fmt.Println(s) } + +func TestSet(t *testing.T) { + b := NewBuffer(10, 10, ' ') + b.Set(11, 0, 'a') +} diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e69de29