Skip to content

Commit b05b993

Browse files
authored
s2: Add block decode fuzzer (#1044)
1 parent aafbabd commit b05b993

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

s2/fuzz_test.go

+61
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,64 @@ func FuzzStreamDecode(f *testing.F) {
182182
}
183183
})
184184
}
185+
186+
func FuzzDecodeBlock(f *testing.F) {
187+
addCompressed := func(b []byte) {
188+
b2 := Encode(nil, b)
189+
f.Add(b2)
190+
f.Add(EncodeBetter(nil, b))
191+
}
192+
fuzz.ReturnFromZip(f, "testdata/enc_regressions.zip", fuzz.TypeRaw, addCompressed)
193+
fuzz.ReturnFromZip(f, "testdata/fuzz/block-corpus-raw.zip", fuzz.TypeRaw, addCompressed)
194+
fuzz.ReturnFromZip(f, "testdata/fuzz/block-corpus-enc.zip", fuzz.TypeGoFuzz, addCompressed)
195+
fuzz.AddFromZip(f, "testdata/dec-block-regressions.zip", fuzz.TypeRaw, false)
196+
197+
f.Fuzz(func(t *testing.T, data []byte) {
198+
if t.Failed() {
199+
return
200+
}
201+
202+
dCopy := append([]byte{}, data...)
203+
dlen, err := DecodedLen(data)
204+
if dlen > 8<<20 {
205+
return
206+
}
207+
base, baseErr := Decode(nil, data)
208+
if !bytes.Equal(data, dCopy) {
209+
t.Fatal("data was changed")
210+
}
211+
hasErr := baseErr != nil
212+
dataCapped := make([]byte, 0, len(data)+1024)
213+
dataCapped = append(dataCapped, data...)
214+
dataCapped = append(dataCapped, bytes.Repeat([]byte{0xff, 0xff, 0xff, 0xff}, 1024/4)...)
215+
dataCapped = dataCapped[:len(data):len(data)]
216+
if dlen > MaxBlockSize {
217+
dlen = MaxBlockSize
218+
}
219+
dst2 := bytes.Repeat([]byte{0xfe}, dlen+1024)
220+
got, err := Decode(dst2[:dlen:dlen], dataCapped[:len(data)])
221+
if !bytes.Equal(dataCapped[:len(data)], dCopy) {
222+
t.Fatal("data was changed")
223+
}
224+
if err != nil && !hasErr {
225+
t.Fatalf("base err: %v, capped: %v", baseErr, err)
226+
}
227+
for i, v := range dst2[dlen:] {
228+
if v != 0xfe {
229+
t.Errorf("DST overwritten beyond cap! index %d: got 0x%02x, want 0x%02x, err:%v", i, v, 0xfe, err)
230+
break
231+
}
232+
}
233+
if baseErr == nil {
234+
if !bytes.Equal(got, base) {
235+
t.Fatal("data mismatch")
236+
}
237+
gotLen, err := DecodedLen(data)
238+
if err != nil {
239+
t.Errorf("DecodedLen returned error: %v", err)
240+
} else if gotLen != len(got) {
241+
t.Errorf("DecodedLen mismatch: got %d, want %d", gotLen, len(got))
242+
}
243+
}
244+
})
245+
}

0 commit comments

Comments
 (0)