]> Cypherpunks.ru repositories - gostls13.git/commitdiff
testing: panic in Fuzz if the function returns a value
authorBryan C. Mills <bcmills@google.com>
Wed, 16 Feb 2022 15:52:01 +0000 (10:52 -0500)
committerBryan Mills <bcmills@google.com>
Wed, 16 Feb 2022 16:06:39 +0000 (16:06 +0000)
Otherwise, the behavior of a fuzz target that returns an error could
be confusing.

Fuzz is already documented to require a function “with no return
value”, so this fixes the implementation to match the existing
documentation.

Fixes #51222

Change-Id: I44ca7ee10960214c92f5ac066ac8484c8bb9cd6f
Reviewed-on: https://go-review.googlesource.com/c/go/+/386175
Trust: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Nooras Saba‎ <saba@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/go/testdata/script/test_fuzz_return.txt [new file with mode: 0644]
src/testing/fuzz.go

diff --git a/src/cmd/go/testdata/script/test_fuzz_return.txt b/src/cmd/go/testdata/script/test_fuzz_return.txt
new file mode 100644 (file)
index 0000000..63275aa
--- /dev/null
@@ -0,0 +1,19 @@
+[short] skip
+
+! go test .
+stdout '^panic: testing: fuzz target must not return a value \[recovered\]$'
+
+-- go.mod --
+module test
+go 1.18
+-- x_test.go --
+package test
+
+import "testing"
+
+func FuzzReturnErr(f *testing.F) {
+       f.Add("hello, validation!")
+       f.Fuzz(func(t *testing.T, in string) string {
+               return in
+       })
+}
index e1d7544f7a1d89f548e21a859af2ee6dd67127a7..b5e1339debd22213ea93f787f92d8e92f65d0a83 100644 (file)
@@ -227,6 +227,9 @@ func (f *F) Fuzz(ff any) {
        if fnType.NumIn() < 2 || fnType.In(0) != reflect.TypeOf((*T)(nil)) {
                panic("testing: fuzz target must receive at least two arguments, where the first argument is a *T")
        }
+       if fnType.NumOut() != 0 {
+               panic("testing: fuzz target must not return a value")
+       }
 
        // Save the types of the function to compare against the corpus.
        var types []reflect.Type