Reject invalid varints when decoding.
Fixes #7.
Fixes #11.
diff --git a/decode.go b/decode.go
index 552a17b..a0d16e1 100644
--- a/decode.go
+++ b/decode.go
@@ -27,7 +27,7 @@
// 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 {
return 0, 0, ErrCorrupt
}
if uint64(int(v)) != v {
diff --git a/snappy_test.go b/snappy_test.go
index 7f1294f..d6a0397 100644
--- a/snappy_test.go
+++ b/snappy_test.go
@@ -78,6 +78,16 @@
}
}
+func TestInvalidVarint(t *testing.T) {
+ data := []byte("\xff\xff\xff\xff\xff\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 {
if len(a) != len(b) {
return fmt.Errorf("got %d bytes, want %d", len(a), len(b))