Skip to content

Commit 45ed3db

Browse files
Romain Bauguebradfitz
Romain Baugue
authored andcommitted
encoding/json: add a Fuzz function
Adds a sample Fuzz test function to package encoding/json following the guidelines defined in #31309, based on https://github.com/dvyukov/go-fuzz-corpus/blob/master/json/json.go Fixes #31309 Updates #19109 Change-Id: I5fe04d9a5f41c0de339f8518dae30896ec14e356 Reviewed-on: https://go-review.googlesource.com/c/go/+/174058 Reviewed-by: Dmitry Vyukov <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> Run-TryBot: Dmitry Vyukov <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 3cf1d77 commit 45ed3db

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/encoding/json/fuzz.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2019 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build gofuzz
6+
7+
package json
8+
9+
import (
10+
"fmt"
11+
)
12+
13+
func Fuzz(data []byte) (score int) {
14+
for _, ctor := range []func() interface{}{
15+
func() interface{} { return new(interface{}) },
16+
func() interface{} { return new(map[string]interface{}) },
17+
func() interface{} { return new([]interface{}) },
18+
} {
19+
v := ctor()
20+
err := Unmarshal(data, v)
21+
if err != nil {
22+
continue
23+
}
24+
score = 1
25+
26+
m, err := Marshal(v)
27+
if err != nil {
28+
fmt.Printf("v=%#v\n", v)
29+
panic(err)
30+
}
31+
32+
u := ctor()
33+
err = Unmarshal(m, u)
34+
if err != nil {
35+
fmt.Printf("v=%#v\n", v)
36+
fmt.Println("m=%s\n", string(m))
37+
panic(err)
38+
}
39+
}
40+
41+
return
42+
}

0 commit comments

Comments
 (0)