From 2ca9c28c5cf9fc1715c57940ad9defe81d0c60c1 Mon Sep 17 00:00:00 2001 From: Gregor Riepl Date: Wed, 5 May 2021 23:35:49 +0200 Subject: [PATCH] Limit slice range --- uncanny/display.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/uncanny/display.go b/uncanny/display.go index 61ab621..bce8f68 100644 --- a/uncanny/display.go +++ b/uncanny/display.go @@ -6,6 +6,22 @@ import ( "golang.org/x/text/encoding/charmap" ) +// min returns the smaller of two integers { +func min(x, y int) int { + if x < y { + return x + } + return y +} + +// max returns the larger of two integers { +func max(x, y int) int { + if x > y { + return x + } + return y +} + // encodeString encodes a Unicode string into a given charmap. // Unsupported characters are replaced by the default replacement symbol. // Mysteriously, this functionality is missing from x/text/encoding. @@ -30,8 +46,8 @@ func (c *Can) DisplayWrite(line int, a []byte) error { } // print in separate chunks, as one frame payload is limited to 7 bytes // the display has only 16 characters per line, so we truncate there - for i := 0; i < len(a) && i < 16; i += 7 { - err := c.bus.Publish(DisplayCommand{addr, a[i:i+7]}.Encode()) + for i := 0; i < min(len(a), 16); i += 7 { + err := c.bus.Publish(DisplayCommand{addr, a[i:min(i+7,len(a))]}.Encode()) if err != nil { return err }