From 59cf85cdc704c27680c348d6f621a6c9fb42e368 Mon Sep 17 00:00:00 2001 From: Louis Thibault Date: Sun, 15 Jan 2023 16:15:18 -0500 Subject: [PATCH 1/2] Add context to 'too many segments' error in decoder. --- message.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/message.go b/message.go index 22124ebe..07467b18 100644 --- a/message.go +++ b/message.go @@ -4,6 +4,7 @@ import ( "bufio" "encoding/binary" "errors" + "fmt" "io" "sync" "sync/atomic" @@ -688,7 +689,8 @@ func (d *Decoder) Decode() (*Message, error) { } maxSeg := SegmentID(binary.LittleEndian.Uint32(d.wordbuf[:])) if maxSeg > maxStreamSegments { - return nil, errors.New("decode: too many segments to decode") + return nil, fmt.Errorf("decode: too many segments (%d) to decode (max=%d)", + maxSeg, maxStreamSegments) } // Read the rest of the header if more than one segment. From 9b3a152ac3bd79e50947cae0ef0779e9c238f2af Mon Sep 17 00:00:00 2001 From: Louis Thibault Date: Fri, 3 Feb 2023 21:27:22 -0500 Subject: [PATCH 2/2] Remove use of fmt package. --- message.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/message.go b/message.go index 07467b18..62446084 100644 --- a/message.go +++ b/message.go @@ -4,7 +4,6 @@ import ( "bufio" "encoding/binary" "errors" - "fmt" "io" "sync" "sync/atomic" @@ -689,8 +688,7 @@ func (d *Decoder) Decode() (*Message, error) { } maxSeg := SegmentID(binary.LittleEndian.Uint32(d.wordbuf[:])) if maxSeg > maxStreamSegments { - return nil, fmt.Errorf("decode: too many segments (%d) to decode (max=%d)", - maxSeg, maxStreamSegments) + return nil, errSegIDTooLarge(maxSeg) } // Read the rest of the header if more than one segment. @@ -758,6 +756,14 @@ func (d *Decoder) Decode() (*Message, error) { return &d.msg, nil } +type errSegIDTooLarge SegmentID + +func (err errSegIDTooLarge) Error() string { + id := str.Utod(err) + max := str.Itod(maxStreamSegments) + return "decode: segment id" + id + "exceeds max segment count (max=" + max + ")" +} + func resizeSlice(b []byte, size int) []byte { if cap(b) < size { return make([]byte, size)