-
Notifications
You must be signed in to change notification settings - Fork 2
/
gofuzz.go
68 lines (60 loc) · 1.15 KB
/
gofuzz.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
61
62
63
64
65
66
67
68
// +build gofuzz
package codf // import "go.spiff.io/codf"
import (
"bytes"
"fmt"
)
func Fuzz(b []byte) (rc int) {
lex := NewLexer(bytes.NewReader(b))
p := NewParser()
p.Parse(lex)
return 0
}
func FuzzLexer(b []byte) (rc int) {
lex := NewLexer(bytes.NewReader(b))
for {
t, err := lex.ReadToken()
if err != nil || t.Kind == TEOF {
return
}
}
}
type fuzzWalker struct{}
func (w *fuzzWalker) Statement(st *Statement) error {
switch st.Name() {
case "server_name",
"listen",
"proxy_pass",
"add_header",
"user",
"daemon",
"log_format",
"acces_log",
"error_log":
return nil
default:
return fmt.Errorf("invalid statement name: %s", st.Name())
}
}
func (w *fuzzWalker) EnterSection(sec *Section) (Walker, error) {
switch sec.Name() {
case "http",
"server",
"location",
"upstream",
"stream":
return w, nil
default:
return nil, fmt.Errorf("invalid section name: %s", sec.Name())
}
}
func FuzzWalker(b []byte) (rc int) {
lex := NewLexer(bytes.NewReader(b))
p := NewParser()
if err := p.Parse(lex); err != nil {
// No panic, but can't use the document
return 0
}
Walk(p.Document(), new(fuzzWalker))
return 0
}