Skip to content

Commit f985833

Browse files
author
Bryan C. Mills
committed
testing: panic in Fuzz if the function returns a value
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 <[email protected]> Run-TryBot: Bryan Mills <[email protected]> Reviewed-by: Robert Findley <[email protected]> Reviewed-by: Nooras Saba‎ <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 5d8d387 commit f985833

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[short] skip
2+
3+
! go test .
4+
stdout '^panic: testing: fuzz target must not return a value \[recovered\]$'
5+
6+
-- go.mod --
7+
module test
8+
go 1.18
9+
-- x_test.go --
10+
package test
11+
12+
import "testing"
13+
14+
func FuzzReturnErr(f *testing.F) {
15+
f.Add("hello, validation!")
16+
f.Fuzz(func(t *testing.T, in string) string {
17+
return in
18+
})
19+
}

src/testing/fuzz.go

+3
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,9 @@ func (f *F) Fuzz(ff any) {
227227
if fnType.NumIn() < 2 || fnType.In(0) != reflect.TypeOf((*T)(nil)) {
228228
panic("testing: fuzz target must receive at least two arguments, where the first argument is a *T")
229229
}
230+
if fnType.NumOut() != 0 {
231+
panic("testing: fuzz target must not return a value")
232+
}
230233

231234
// Save the types of the function to compare against the corpus.
232235
var types []reflect.Type

0 commit comments

Comments
 (0)