You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
math/rand: NewZipf accepts non-finite parameters that prevent Uint64 from returning #81492
Both math/rand.NewZipf and math/rand/v2.NewZipf accept NaN parameters because their range checks use comparisons that are false for NaN. The resulting Uint64 rejection loop cannot satisfy either exit condition. Positive infinity in either parameter can also produce a nonterminating sampler.
z:=rand.NewZipf(rand.New(rand.NewPCG(1, 2)), math.NaN(), 1, 10) // rand is math/rand/v2z.Uint64() // does not return
Details
Both src/math/rand/zipf.go and src/math/rand/v2/zipf.go check s <= 1.0 || v < 1, despite documenting s > 1 and v >= 1. NaN passes this check and propagates into the derived state, making both k-x <= z.s and ur >= z.h(k+0.5)-math.Exp(-math.Log(k+z.v)*z.q) false on every iteration.
For math/rand, the same example uses rand.New(rand.NewSource(1)). Other cases to cover are v = NaN, s = +Inf, and v = +Inf.
This issue was found by zkao and was triaged by @FiloSottile as a valid bug but not a vulnerability. /cc @golang/security
I'd like to work on this. My proposed scope is to reject NaN and infinite s or v by returning nil from NewZipf in both math/rand and math/rand/v2, and add regression tests for those inputs and valid boundary cases. Does that behavior sound right, and is anyone already working on it?
Activity
gabyhelp commentedon Sep 13, 2026
Related Issues
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
cristiangirlea commentedon Sep 16, 2026
I'd like to work on this. My proposed scope is to reject NaN and infinite s or v by returning nil from NewZipf in both math/rand and math/rand/v2, and add regression tests for those inputs and valid boundary cases. Does that behavior sound right, and is anyone already working on it?
gopherbot commentedon Sep 16, 2026
Change https://go.dev/cl/833565 mentions this issue:
math/rand: reject non-finite Zipf parameters