]> Cypherpunks repositories - gostls13.git/commitdiff
internal/fuzz: improve error for mismatched types
authorKatie Hockman <katie@golang.org>
Wed, 3 Nov 2021 14:58:59 +0000 (10:58 -0400)
committerKatie Hockman <katie@golang.org>
Wed, 3 Nov 2021 16:04:43 +0000 (16:04 +0000)
Fixes #48635

Change-Id: Ia3cde119d5eb31bc771fe3a39acb2372dbd988ff
Reviewed-on: https://go-review.googlesource.com/c/go/+/361114
Trust: Katie Hockman <katie@golang.org>
Trust: Daniel Martí <mvdan@mvdan.cc>
Run-TryBot: Katie Hockman <katie@golang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
src/cmd/go/testdata/script/test_fuzz.txt
src/internal/fuzz/fuzz.go

index 020012d73e530b7add21c283bac72f50682f465e..150491be042d487975e07f2ef008fcb493294a33 100644 (file)
@@ -152,6 +152,7 @@ stdout FAIL
 # Test that the wrong type given with f.Add will fail.
 ! go test -run FuzzWrongType fuzz_add_test.go
 ! stdout ^ok
+stdout '\[string int\], want \[\[\]uint8 int8\]'
 stdout FAIL
 
 # Test fatal with testdata seed corpus
@@ -435,8 +436,8 @@ func FuzzAddDifferentType(f *testing.F) {
 }
 
 func FuzzWrongType(f *testing.F) {
-    f.Add("hello")
-    f.Fuzz(func(*testing.T, []byte) {})
+    f.Add("hello", 50)
+    f.Fuzz(func(*testing.T, []byte, int8) {})
 }
 
 -- corpustesting/fuzz_testdata_corpus_test.go --
index 2ebe2a64db637d0e0e3ff51d170028d39927dabe..aef1dee9783a828bb43855828c00d8a28dfd6511 100644 (file)
@@ -997,11 +997,15 @@ func readCorpusData(data []byte, types []reflect.Type) ([]interface{}, error) {
 // provided.
 func CheckCorpus(vals []interface{}, types []reflect.Type) error {
        if len(vals) != len(types) {
-               return fmt.Errorf("wrong number of values in corpus entry %v: want %v", vals, types)
+               return fmt.Errorf("wrong number of values in corpus entry: %d, want %d", len(vals), len(types))
+       }
+       valsT := make([]reflect.Type, len(vals))
+       for valsI, v := range vals {
+               valsT[valsI] = reflect.TypeOf(v)
        }
        for i := range types {
-               if reflect.TypeOf(vals[i]) != types[i] {
-                       return fmt.Errorf("mismatched types in corpus entry: %v, want %v", vals, types)
+               if valsT[i] != types[i] {
+                       return fmt.Errorf("mismatched types in corpus entry: %v, want %v", valsT, types)
                }
        }
        return nil