]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.fuzz] internal/fuzz: fix panic when marshaling data
authorKatie Hockman <katie@golang.org>
Wed, 8 Sep 2021 18:06:52 +0000 (14:06 -0400)
committerKatie Hockman <katie@golang.org>
Wed, 8 Sep 2021 19:16:20 +0000 (19:16 +0000)
The coordinator needs to marshal data that was provided
via f.Add. However, it was also attempting to marshal data
that was in testdata, which was not needed,
and was causing a panic. This change fixes this.

Fixes golang/go#48228

Change-Id: I1256c5a287b5a09d2f8cca59beb0f0fc06cc3554
Reviewed-on: https://go-review.googlesource.com/c/go/+/348381
Trust: Katie Hockman <katie@golang.org>
Run-TryBot: Katie Hockman <katie@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
src/cmd/go/testdata/script/test_fuzz.txt
src/internal/fuzz/fuzz.go

index d3c7b4d55fb2f1afd0223700cfd10f9ef95e522c..a25f0fba3f4f4e5130ab503b6459044ad6ec0a44 100644 (file)
@@ -160,6 +160,21 @@ stdout ok
 ! stdout FAIL
 ! stdout 'fatal here'
 
+# Test pass with testdata and f.Add seed corpus
+go test -run FuzzPassString corpustesting/fuzz_testdata_corpus_test.go
+stdout ok
+! stdout FAIL
+
+# Fuzzing pass with testdata and f.Add seed corpus (skip running tests first)
+go test -run=None -fuzz=FuzzPassString corpustesting/fuzz_testdata_corpus_test.go -fuzztime=10x
+stdout ok
+! stdout FAIL
+
+# Fuzzing pass with testdata and f.Add seed corpus
+go test -run=FuzzPassString -fuzz=FuzzPassString corpustesting/fuzz_testdata_corpus_test.go -fuzztime=10x
+stdout ok
+! stdout FAIL
+
 # Test panic with malformed seed corpus
 ! go test -run FuzzFail corpustesting/fuzz_testdata_corpus_test.go
 ! stdout ^ok
@@ -413,6 +428,11 @@ func FuzzPass(f *testing.F) {
     fuzzFn(f)
 }
 
+func FuzzPassString(f *testing.F) {
+    f.Add("some seed corpus")
+    f.Fuzz(func(*testing.T, string) {})
+}
+
 func FuzzPanic(f *testing.F) {
     f.Fuzz(func(t *testing.T, b []byte) {})
 }
@@ -431,6 +451,9 @@ go test fuzz v1
 -- corpustesting/testdata/corpus/FuzzPass/1 --
 go test fuzz v1
 []byte("00000")
+-- corpustesting/testdata/corpus/FuzzPassString/1 --
+go test fuzz v1
+string("hello")
 -- corpustesting/testdata/corpus/FuzzPanic/1 --
 malformed
 -- corpustesting/testdata/corpus/FuzzInNestedDir/anotherdir/1 --
index 5b940e4929b81e838e7293220a2d75e28c497f86..f36569b4cc595d47d1ec72b57501f43668e3ccad 100644 (file)
@@ -576,9 +576,9 @@ type coordinator struct {
 }
 
 func newCoordinator(opts CoordinateFuzzingOpts) (*coordinator, error) {
-       // Make sure all of the seed corpus has marshalled data.
+       // Make sure all of the seed corpus given by f.Add has marshalled data.
        for i := range opts.Seed {
-               if opts.Seed[i].Data == nil {
+               if opts.Seed[i].Data == nil && opts.Seed[i].Values != nil {
                        opts.Seed[i].Data = marshalCorpusFile(opts.Seed[i].Values...)
                }
        }