fix: properly handle missing size bytes

This commit is contained in:
Derrick Hammer 2023-06-11 01:38:19 -04:00
parent 385a51e504
commit c0df04d7d5
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 14 additions and 3 deletions

View File

@ -10,6 +10,11 @@ import (
var MAGIC_BYTES = []byte{0x26, 0x1f} var MAGIC_BYTES = []byte{0x26, 0x1f}
var (
ErrMissingEmptySize = errors.New("Missing or empty size")
ErrInvalidCIDMagic = errors.New("CID magic bytes missing or invalid")
)
type CID struct { type CID struct {
Hash [32]byte Hash [32]byte
Size uint64 Size uint64
@ -76,13 +81,19 @@ func maybeDecode(cid string) ([]byte, error) {
} }
if bytes.Compare(data[0:len(MAGIC_BYTES)], MAGIC_BYTES) != 0 { if bytes.Compare(data[0:len(MAGIC_BYTES)], MAGIC_BYTES) != 0 {
return nil, errors.New("CID magic bytes missing or invalid") return nil, ErrInvalidCIDMagic
} }
size := binary.LittleEndian.Uint64(data[len(MAGIC_BYTES)+32:]) sizeBytes := data[len(MAGIC_BYTES)+32:]
if len(sizeBytes) == 0 {
return nil, ErrMissingEmptySize
}
size := binary.LittleEndian.Uint64(sizeBytes)
if size == 0 { if size == 0 {
return nil, errors.New("missing or empty size") return nil, ErrMissingEmptySize
} }
return data, nil return data, nil