Skip to content

fix(random): MT19937.SetState refuses a position outside [0, 624] - #134

Merged
tannevaled merged 4 commits into
mainfrom
fix/mt19937-setstate-pos
Nov 29, 2026
Merged

tannevaled merged 4 commits into
mainfrom
fix/mt19937-setstate-pos

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

What

MT19937.SetState (random/mt19937.go) stored Pos as given. Uint32 regenerates the key only when pos == 624, so a negative Pos, or one above 624, indexed the 624-word key out of range. genNext and addState, which Jumped reaches, did the same. go-embedded-r found this while binding R's RNG.

  • MT19937.SetState now returns an error for a Pos outside [0, 624] and leaves the generator unchanged.
  • RandomState.SetState passes that error on.

API: SetState gains an error result. Calls that use it as a statement still compile. No interface in the package includes SetState.

Tests

  • TestMT19937SetStateRefusesPosOutOfRange: Pos -1, -624, 625 and 2^40 are each refused. The state is kept and a draw still works. The same holds through RandomState.
  • TestMT19937SetStateAcceptsBothEnds: Pos 0 and Pos 624 are accepted and continue the stream draw for draw. The test asserts that both ends were actually reached.
  • FuzzMT19937SetState: over Pos and the key words, SetState either refuses, or leaves a generator that can draw, jump and draw again. It ran for 20 s locally with no failure; the seeds are in f.Add.
  • mutsweep on the two changed refusals:
    • mt19937.go:166 is caught by an assertion. With the guard disabled, Uint32 panics with index out of range [-1], which is the reported bug.
    • legacy.go:109 is caught by an assertion.

Performance

SetState runs once per state transfer, outside any draw loop (docs/perf-exempt.md:49). Uint32 is unchanged.

Checks run locally: gofmt -l prints nothing and go test ./random/ is green.

🤖 Generated with Claude Code

tannevaled and others added 2 commits November 29, 2026 08:16
SetState stored Pos as given. Uint32 regenerates the key only when
pos == 624, so a negative Pos or one above 624 indexed the 624-word key
out of range (and genNext/addState, reached through Jumped, as well).
Reported by go-embedded-r while binding R's RNG.

SetState now returns an error for such a Pos and leaves the generator
unchanged; RandomState.SetState passes it on. Calls that ignored the
(previously absent) result still compile.

Tests: both bounds refused (-1, -624, 625, 2^40) with the state kept and
a draw still safe; Pos 0 and 624 accepted and continuing the stream;
FuzzMT19937SetState over Pos and key words (20 s locally, no failure).
With the guard disabled the refusal test fails and Uint32 panics with
index out of range [-1].

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 << 40 overflows int on 386 and arm, so the test file did not compile on
the 32-bit lanes. Use math.MaxInt and math.MinInt, which also cover both
extremes on every word size.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tannevaled
tannevaled force-pushed the fix/mt19937-setstate-pos branch from 2fef3a5 to c1cc030 Compare November 29, 2026 07:16
tannevaled and others added 2 commits November 29, 2026 08:18
…tState errors

From the review of #134:
- the refusal test now also flips HasGauss/Gauss in the refused state and
  asserts that GetState after the refusal equals the state before it (NumPy
  writes gauss before the bit-generator setter refuses; we must not);
- api_test.go checks the error that MT19937.SetState now returns.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit f5e4e19 into main Nov 29, 2026
13 checks passed
@tannevaled
tannevaled deleted the fix/mt19937-setstate-pos branch November 29, 2026 07:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant