Skip to content

Commit

Permalink
Merge pull request #16 from dgryski/uint32-fix
Browse files Browse the repository at this point in the history
Fix max block size check
  • Loading branch information
nigeltao committed Jul 30, 2015
2 parents 0c7f8a7 + f5466f1 commit 723cc1e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
10 changes: 7 additions & 3 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
var (
// ErrCorrupt reports that the input is invalid.
ErrCorrupt = errors.New("snappy: corrupt input")
// ErrTooLarge reports that the uncompressed length is too large.
ErrTooLarge = errors.New("snappy: decoded block is too large")
// ErrUnsupported reports that the input isn't supported.
ErrUnsupported = errors.New("snappy: unsupported input")
)
Expand All @@ -27,11 +29,13 @@ func DecodedLen(src []byte) (int, error) {
// that the length header occupied.
func decodedLen(src []byte) (blockLen, headerLen int, err error) {
v, n := binary.Uvarint(src)
if n <= 0 {
if n <= 0 || v > 0xffffffff {
return 0, 0, ErrCorrupt
}
if uint64(int(v)) != v {
return 0, 0, errors.New("snappy: decoded block is too large")

const wordSize = 32 << (^uint(0) >> 32 & 1)
if wordSize == 32 && v > 0x7fffffff {
return 0, 0, ErrTooLarge
}
return int(v), n, nil
}
Expand Down
10 changes: 10 additions & 0 deletions snappy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ func TestInvalidVarint(t *testing.T) {
if _, err := Decode(nil, data); err != ErrCorrupt {
t.Errorf("Decode: got %v, want ErrCorrupt", err)
}

// The encoded varint overflows 32 bits
data = []byte("\xff\xff\xff\xff\xff\x00")

if _, err := DecodedLen(data); err != ErrCorrupt {
t.Errorf("DecodedLen: got %v, want ErrCorrupt", err)
}
if _, err := Decode(nil, data); err != ErrCorrupt {
t.Errorf("Decode: got %v, want ErrCorrupt", err)
}
}

func cmp(a, b []byte) error {
Expand Down

0 comments on commit 723cc1e

Please sign in to comment.