aboutsummaryrefslogtreecommitdiff
path: root/src/encoding
diff options
context:
space:
mode:
authorRoland Shoemaker <roland@golang.org>2021-08-25 13:50:24 -0700
committerRoland Shoemaker <roland@golang.org>2022-01-13 18:06:33 +0000
commit4fa6e33f30365a8bca374ab8bd47acd82b9faa96 (patch)
treededcf95c3f1a15058bbc576dd610ec43c9351f47 /src/encoding
parent24239120bfbff9ebee8e8c344d9d3a8ce460b686 (diff)
downloadgo-4fa6e33f30365a8bca374ab8bd47acd82b9faa96.tar.gz
go-4fa6e33f30365a8bca374ab8bd47acd82b9faa96.zip
all: add a handful of fuzz targets
Adds simple fuzz targets to archive/tar, archive/zip, compress/gzip, encoding/json, image/jpeg, image/gif, and image/png. Second attempt, this time we don't use the archives in testdata when fuzzing archive/tar, since those are rather memory intensive, and were crashing a number of builders. Change-Id: I4828d64fa4763c0d8c980392a6578e4dfd956e13 Reviewed-on: https://go-review.googlesource.com/c/go/+/378174 Trust: Roland Shoemaker <roland@golang.org> Run-TryBot: Roland Shoemaker <roland@golang.org> Reviewed-by: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/encoding')
-rw-r--r--src/encoding/json/fuzz_test.go83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/encoding/json/fuzz_test.go b/src/encoding/json/fuzz_test.go
new file mode 100644
index 0000000000..778664c3e5
--- /dev/null
+++ b/src/encoding/json/fuzz_test.go
@@ -0,0 +1,83 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package json
+
+import (
+ "bytes"
+ "io"
+ "testing"
+)
+
+func FuzzUnmarshalJSON(f *testing.F) {
+ f.Add([]byte(`{
+"object": {
+ "slice": [
+ 1,
+ 2.0,
+ "3",
+ [4],
+ {5: {}}
+ ]
+},
+"slice": [[]],
+"string": ":)",
+"int": 1e5,
+"float": 3e-9"
+}`))
+
+ f.Fuzz(func(t *testing.T, b []byte) {
+ for _, typ := range []func() interface{}{
+ func() interface{} { return new(interface{}) },
+ func() interface{} { return new(map[string]interface{}) },
+ func() interface{} { return new([]interface{}) },
+ } {
+ i := typ()
+ if err := Unmarshal(b, i); err != nil {
+ return
+ }
+
+ encoded, err := Marshal(i)
+ if err != nil {
+ t.Fatalf("failed to marshal: %s", err)
+ }
+
+ if err := Unmarshal(encoded, i); err != nil {
+ t.Fatalf("failed to roundtrip: %s", err)
+ }
+ }
+ })
+}
+
+func FuzzDecoderToken(f *testing.F) {
+ f.Add([]byte(`{
+"object": {
+ "slice": [
+ 1,
+ 2.0,
+ "3",
+ [4],
+ {5: {}}
+ ]
+},
+"slice": [[]],
+"string": ":)",
+"int": 1e5,
+"float": 3e-9"
+}`))
+
+ f.Fuzz(func(t *testing.T, b []byte) {
+ r := bytes.NewReader(b)
+ d := NewDecoder(r)
+ for {
+ _, err := d.Token()
+ if err != nil {
+ if err == io.EOF {
+ break
+ }
+ return
+ }
+ }
+ })
+}