Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
Fix bug where NilChance(0) could still result in nil (#47)
Browse files Browse the repository at this point in the history
* Update genShouldFill to use greater-then-or-equal

Because rand.Float64 can return 0 it was possible for this function to 
return false when the nilChance is set to 0 (because 0 > 0 = false).

* Add test case for NilChance(0) against "random" zero
  • Loading branch information
ericcornelissen committed Aug 4, 2020
1 parent 18d1906 commit 379e164
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion fuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (f *Fuzzer) genElementCount() int {
}

func (f *Fuzzer) genShouldFill() bool {
return f.r.Float64() > f.nilChance
return f.r.Float64() >= f.nilChance
}

// MaxDepth sets the maximum number of recursive fuzz calls that will be made
Expand Down
29 changes: 29 additions & 0 deletions fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,35 @@ func TestFuzz_SkipPattern(t *testing.T) {
})
}

func TestFuzz_NilChanceZero(t *testing.T) {
// This data source for random will result in the following four values
// being sampled (the first, 0, being the most interesting case):
// 0; 0.8727288671879787; 0.5547307616625858; 0.021885026049502695
data := []byte("H0000000\x00")
f := NewFromGoFuzz(data).NilChance(0)

var fancyStruct struct {
A, B, C, D *string
}
f.Fuzz(&fancyStruct) // None of the pointers should be nil, as NilChance is 0

if fancyStruct.A == nil {
t.Error("First value in struct was nil")
}

if fancyStruct.B == nil {
t.Error("Second value in struct was nil")
}

if fancyStruct.C == nil {
t.Error("Third value in struct was nil")
}

if fancyStruct.D == nil {
t.Error("Fourth value in struct was nil")
}
}

type int63mode int

const (
Expand Down

0 comments on commit 379e164

Please sign in to comment.