aboutsummaryrefslogtreecommitdiff
path: root/src/internal/fuzz
diff options
context:
space:
mode:
authorKatie Hockman <katie@golang.org>2021-04-27 14:34:17 -0400
committerKatie Hockman <katie@golang.org>2021-05-04 18:57:18 +0000
commitaf3237eaf9cf46e6a02a3b53447e49c55abd4f00 (patch)
tree6bbedd796ffdc4fcbbcd8c85221ba34e861c9d80 /src/internal/fuzz
parentfe8c0e9467d8628138d54951ebb8e166c086c80b (diff)
downloadgo-af3237eaf9cf46e6a02a3b53447e49c55abd4f00.tar.xz
[dev.fuzz] internal/fuzz: don't panic if types change
There was a bug where if the types to fuzz were different from the types in a file in the on-disk corpus, then the code would panic. We thought this case was handled, but the final `continue` in the nested loop still allowed the invalid entry to be added to the corpus. Pulling the validation into a helper function makes this less brittle. Change-Id: I401346f890ea30ab7cff9640cb555da2e3ff8cc6 Reviewed-on: https://go-review.googlesource.com/c/go/+/313810 Trust: Katie Hockman <katie@golang.org> Run-TryBot: Katie Hockman <katie@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Jay Conrod <jayconrod@google.com>
Diffstat (limited to 'src/internal/fuzz')
-rw-r--r--src/internal/fuzz/fuzz.go31
1 files changed, 19 insertions, 12 deletions
diff --git a/src/internal/fuzz/fuzz.go b/src/internal/fuzz/fuzz.go
index 586b51188c..b9d2d4cd5f 100644
--- a/src/internal/fuzz/fuzz.go
+++ b/src/internal/fuzz/fuzz.go
@@ -506,21 +506,12 @@ func ReadCorpus(dir string, types []reflect.Type) ([]CorpusEntry, error) {
if err != nil {
return nil, fmt.Errorf("failed to read corpus file: %v", err)
}
- vals, err := unmarshalCorpusFile(data)
+ var vals []interface{}
+ vals, err = readCorpusData(data, types)
if err != nil {
- errs = append(errs, fmt.Errorf("failed to unmarshal %q: %v", filename, err))
- continue
- }
- if len(vals) != len(types) {
- errs = append(errs, fmt.Errorf("wrong number of values in corpus file %q: %d, want %d", filename, len(vals), len(types)))
+ errs = append(errs, fmt.Errorf("%q: %v", filename, err))
continue
}
- for i := range types {
- if reflect.TypeOf(vals[i]) != types[i] {
- errs = append(errs, fmt.Errorf("mismatched types in corpus file %q: %v, want %v", filename, vals, types))
- continue
- }
- }
corpus = append(corpus, CorpusEntry{Name: file.Name(), Data: data, Values: vals})
}
if len(errs) > 0 {
@@ -529,6 +520,22 @@ func ReadCorpus(dir string, types []reflect.Type) ([]CorpusEntry, error) {
return corpus, nil
}
+func readCorpusData(data []byte, types []reflect.Type) ([]interface{}, error) {
+ vals, err := unmarshalCorpusFile(data)
+ if err != nil {
+ return nil, fmt.Errorf("unmarshal: %v", err)
+ }
+ if len(vals) != len(types) {
+ return nil, fmt.Errorf("wrong number of values in corpus file: %d, want %d", len(vals), len(types))
+ }
+ for i := range types {
+ if reflect.TypeOf(vals[i]) != types[i] {
+ return nil, fmt.Errorf("mismatched types in corpus file: %v, want %v", vals, types)
+ }
+ }
+ return vals, nil
+}
+
// writeToCorpus atomically writes the given bytes to a new file in testdata.
// If the directory does not exist, it will create one. If the file already
// exists, writeToCorpus will not rewrite it. writeToCorpus returns the