forked from dvyukov/go-fuzz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
123 lines (103 loc) · 1.85 KB
/
main.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2015 Dmitry Vyukov. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package texttemplate
import (
"errors"
"io/ioutil"
"regexp"
"text/template"
)
// Huge padding
var fmtHang = regexp.MustCompile("%[-+# 0]*(((([0-9]{3,})|[*]))|([0-9]*\\.(([0-9]{3,})|[*])))")
func Fuzz(data []byte) int {
if fmtHang.Match(data) {
return 0
}
t, err := template.New("foo").Funcs(funcs).Parse(string(data))
if err != nil {
if t != nil {
panic("non nil template on error")
}
return 0
}
d := &Data{
A: 42,
B: "foo",
C: []int{1, 2, 3},
D: map[int]string{1: "foo", 2: "bar"},
E: Data1{42, "foo"},
}
defer func() {
x := recover()
if x != nil {
/*
if str, ok := x.(string); ok && str == "unidentified node type in allIdents" {
return
}
*/
panic(x)
}
}()
t.Execute(ioutil.Discard, d)
return 1
}
type Data struct {
A int
B string
C []int
D map[int]string
E Data1
}
type Data1 struct {
A int
B string
}
func (Data1) Q() string {
return "foo"
}
func (Data1) W() (string, error) {
return "foo", nil
}
func (Data1) E() (string, error) {
return "foo", errors.New("Data.E error")
}
func (Data1) R(v int) (string, error) {
return "foo", nil
}
func (Data1) T(s string) (string, error) {
return s, nil
}
var funcs = map[string]interface{}{
"Q": func1,
"W": func2,
"E": func3,
"R": func4,
"T": func5,
"Y": func6,
"U": func7,
"I": func8,
}
func func1(s string) string {
return s
}
func func2(s string) (string, error) {
return s, nil
}
func func3(s string) (string, error) {
return s, errors.New("func3 error")
}
func func4(v int) int {
return v
}
func func5(v int) (int, error) {
return v, nil
}
func func6() int {
return 42
}
func func7() (int, error) {
return 42, nil
}
func func8() (int, error) {
return 42, errors.New("func8 error")
}