Skip to content

Commit

Permalink
Merge pull request #431 from capnproto/improve-maxseg-error
Browse files Browse the repository at this point in the history
Add context to 'too many segments' error in decoder.
  • Loading branch information
lthibault authored Feb 4, 2023
2 parents b138dc0 + 9b3a152 commit e9a1ca6
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion message.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,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 @@ -804,6 +804,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

0 comments on commit e9a1ca6

Please sign in to comment.