Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add context to 'too many segments' error in decoder. #431

Merged
merged 2 commits into from
Feb 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion message.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ 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, errSegIDTooLarge(maxSeg)
}

// Read the rest of the header if more than one segment.
Expand Down Expand Up @@ -756,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)
Expand Down