Skip to content

Commit

Permalink
fix: do bound check while checking for CIDv0
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorropo authored and masih committed Jul 6, 2022
1 parent a35f048 commit 24feaad
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ type BytesReader interface {

// TODO: this belongs in the go-cid package
func ReadCid(buf []byte) (cid.Cid, int, error) {
if bytes.Equal(buf[:2], cidv0Pref) {
c, err := cid.Cast(buf[:34])
return c, 34, err
if len(buf) >= 2 && bytes.Equal(buf[:2], cidv0Pref) {
i := 34
if len(buf) < i {
i = len(buf)
}
c, err := cid.Cast(buf[:i])
return c, i, err
}

br := bytes.NewReader(buf)
Expand Down

0 comments on commit 24feaad

Please sign in to comment.