Skip to content

math/rand: NewZipf accepts non-finite parameters that prevent Uint64 from returning #81492

Open

Description

@filippo-agent

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/v2
z.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

Activity

  1. cristiangirlea commented on Sep 16, 2026

    @cristiangirlea
    Contributor

    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?

  2. gopherbot commented on Sep 16, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/833565 mentions this issue: math/rand: reject non-finite Zipf parameters

  3. added this to the Go1.28 milestone on Sep 18, 2026
  4. added
    FixPendingIssues that have a fix which has not yet been reviewed or submitted.
    on Sep 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixPendingIssues that have a fix which has not yet been reviewed or submitted.

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions