-
Notifications
You must be signed in to change notification settings - Fork 48
/
gzip.go
60 lines (57 loc) · 1.15 KB
/
gzip.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2015 go-fuzz project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package gzip
import (
"bytes"
"compress/gzip"
"io"
"io/ioutil"
)
func Fuzz(data []byte) int {
fr, err := gzip.NewReader(bytes.NewReader(data))
if err != nil {
return 0
}
if len(fr.Comment) > 1<<20 || len(fr.Name) > 1<<20 || len(fr.Extra) > 1<<20 {
panic("huge header")
}
uncomp := make([]byte, 64<<10)
n, err := fr.Read(uncomp)
if err != nil && err != io.EOF {
return 0
}
if n == len(uncomp) {
return 0 // too large
}
uncomp = uncomp[:n]
for c := 0; c <= 9; c++ {
buf := new(bytes.Buffer)
gw, err := gzip.NewWriterLevel(buf, c)
if err != nil {
panic(err)
}
gw.Header = fr.Header
n, err := gw.Write(uncomp)
if err != nil {
panic(err)
}
if n != len(uncomp) {
panic("short write")
}
if err := gw.Close(); err != nil {
panic(err)
}
fr1, err := gzip.NewReader(buf)
if err != nil {
panic(err)
}
uncomp1, err := ioutil.ReadAll(fr1)
if err != nil {
panic(err)
}
if !bytes.Equal(uncomp, uncomp1) {
panic("data differs")
}
}
return 1
}