From f98b551ba26de9a0e1a4d555dcf0de780b59800b Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sun, 6 Nov 2022 15:25:55 -0500 Subject: [PATCH] Added bounds checking to buffer --- buffer.go | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/buffer.go b/buffer.go index 9bf6dec..abc9593 100644 --- a/buffer.go +++ b/buffer.go @@ -49,6 +49,15 @@ type Buffer struct { } } +func (buffer *Buffer) isOutOfBounds (x, y int) (outOfBounds bool) { + outOfBounds = + x < 0 || + y < 0 || + x >= buffer.width || + y >= buffer.height + return +} + func (buffer *Buffer) Size () (width, height int) { width = buffer.width height = buffer.height @@ -62,19 +71,23 @@ func (buffer *Buffer) SetSize (width, height int) { } func (buffer *Buffer) Cell (x, y int) (cell Cell) { + if buffer.isOutOfBounds(x, y) { return } cell = buffer.content[x + y * buffer.width] return } func (buffer *Buffer) SetColor (x, y int, color Color) { + if buffer.isOutOfBounds(x, y) { return } buffer.content[x + y * buffer.width].color = color } func (buffer *Buffer) SetStyle (x, y int, style Style) { + if buffer.isOutOfBounds(x, y) { return } buffer.content[x + y * buffer.width].style = style } func (buffer *Buffer) SetRune (x, y int, content rune) { + if buffer.isOutOfBounds(x, y) { return } buffer.content[x + y * buffer.width].content = content } @@ -110,18 +123,24 @@ func (buffer *DamageBuffer) SetSize (width, height int) { } func (buffer *DamageBuffer) SetColor (x, y int, color Color) { + if buffer.isOutOfBounds(x, y) { return } + index := x + y * buffer.width + buffer.clean[index] = buffer.content[index].color == color buffer.Buffer.SetColor(x, y, color) - buffer.clean[x + y * buffer.width] = false } func (buffer *DamageBuffer) SetStyle (x, y int, style Style) { + if buffer.isOutOfBounds(x, y) { return } + index := x + y * buffer.width + buffer.clean[index] = buffer.content[index].style == style buffer.Buffer.SetStyle(x, y, style) - buffer.clean[x + y * buffer.width] = false } func (buffer *DamageBuffer) SetRune (x, y int, content rune) { + if buffer.isOutOfBounds(x, y) { return } + index := x + y * buffer.width + buffer.clean[index] = buffer.content[index].content == content buffer.Buffer.SetRune(x, y, content) - buffer.clean[x + y * buffer.width] = false } func (buffer *DamageBuffer) Write (bytes []byte) (bytesWritten int, err error) { @@ -141,10 +160,12 @@ func (buffer *DamageBuffer) Write (bytes []byte) (bytesWritten int, err error) { } func (buffer *DamageBuffer) Clean (x, y int) (clean bool) { + if buffer.isOutOfBounds(x, y) { return } clean = buffer.clean[x + y * buffer.width] return } func (buffer *DamageBuffer) MarkClean (x, y int) { + if buffer.isOutOfBounds(x, y) { return } buffer.clean[x + y * buffer.width] = true }