-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathjson.go
91 lines (82 loc) · 1.68 KB
/
json.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
// 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 json
import (
"encoding/json"
"fmt"
"github.com/dvyukov/go-fuzz-corpus/fuzz"
)
func Fuzz(data []byte) int {
score := 0
for _, ctor := range []func() interface{}{
func() interface{} { return nil },
func() interface{} { return new([]interface{}) },
func() interface{} { m := map[string]string{}; return &m },
func() interface{} { m := map[string]interface{}{}; return &m },
func() interface{} { return new(S) },
} {
v := ctor()
if json.Unmarshal(data, v) != nil {
continue
}
score = 1
if s, ok := v.(*S); ok {
if len(s.P) == 0 {
s.P = []byte(`""`)
}
}
data1, err := json.Marshal(v)
if err != nil {
panic(err)
}
v1 := ctor()
if json.Unmarshal(data1, v1) != nil {
continue
}
if s, ok := v.(*S); ok {
// Some additional escaping happens with P.
s.P = nil
v1.(*S).P = nil
}
if !fuzz.DeepEqual(v, v1) {
fmt.Printf("v0: %#v\n", v)
fmt.Printf("v1: %#v\n", v1)
panic("not equal")
}
}
return score
}
type S struct {
A int `json:",omitempty"`
B string `json:"B1,omitempty"`
C float64
D bool
E uint8
F []byte
G interface{}
H map[string]interface{}
I map[string]string
J []interface{}
K []string
L S1
M *S1
N *int
O **int
P json.RawMessage
Q Marshaller
R int `json:"-"`
S int `json:",string"`
}
type S1 struct {
A int
B string
}
type Marshaller struct {
v string
}
func (m *Marshaller) MarshalJSON() ([]byte, error) {
return json.Marshal(m.v)
}
func (m *Marshaller) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &m.v)
}