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
[stdlib] random.range_i32/chance panic on INT32_MIN draw (checked 0 - v overflow) #1058
std.random.range_i32 / chance (lib/std/random.w:44-57) abort the
process with panic: integer overflow when the underlying draw is
exactly INT32_MIN (probability 2^-32 per call): both compute 0 - v with checked subtraction, and 0 - INT32_MIN overflows.
var v: i32 = -2147483648; print_i32(0 - v) → panic: integer overflow: i32 subtraction out of range (checked - confirmed).
next_i32() returns (xorshift64_state >> 16) as i32, which spans
the full i32 range including MIN (5/5 seeded outputs verified
against an independent signed-xorshift python oracle, so the draw
distribution genuinely covers MIN).
Affected lines: range_i32 (:48: let positive = if v < 0: 0 - v else: v) and chance (:55, same shape). No in-repo callers
today, but this is pub stdlib API.
Notes (not applied; audit is read-only)
Use wrapping negation (0 -% v, mapping MIN to MIN, still uniform
over the range expression) or guard MIN explicitly. Suggested
regression: direct unit probe of the MIN input through both
functions (must not panic).
Verdict. Established. Both range_i32 and chance compute let positive = if v < 0: 0 - v else: v, and 0 - INT32_MIN overflows under checked subtraction, aborting the process with panic: integer overflow: i32 subtraction out of range. next_i32 can draw INT32_MIN, so the panic fires with probability 2^-32 per call. The report's wrapping-negation fix is correct.
Tested.
Source
main @ 5809ac1 (2026-09-14); lib/std/random.w:46-60 unchanged since the reported revision 450733e
Compilers
with v0.15.2.1 built from 5809ac1 (staging worktree); a compiler built from 450733e with the nightly-20260903 seed. Same panic
Platform
macOS Darwin 25.6.0 arm64
Repros (with run), both compilers:
Program
Result
print(0 - v) with var v: i32 = -2147483648
panic: integer overflow: i32 subtraction out of range
range_i32 shape with v = INT32_MIN, range_i32(_, 0, 10)
same panic
same with v = -5 (control)
5
print(0 -% v) with v = INT32_MIN (proposed fix)
-2147483648, no panic
Source pins (5809ac1).range_i32 at lib/std/random.w:50 (let positive = if v < 0: 0 - v else: v), chance at :58, same shape. Draw source next_i32 at :41-43: (xorshift64() >> 16) as i32.
Reachability of the MIN draw.next_i32 truncates (xorshift64() >> 16) to i32. xorshift64 has full period over its nonzero 64-bit state, so the low 32 bits of (state >> 16) take every value in [0, 2^32), including 0x80000000 = INT32_MIN. So the input that overflows is genuinely producible (probability ~2^-32 per call). The report verified the same by oracle; I established it by the generator's structure rather than brute-forcing a 1-in-4-billion seed (see blockers).
Root cause. Checked-arithmetic overflow on a reachable input; 0 - INT32_MIN has no representable i32 result. No compiler defect — the checked - is doing its job; the stdlib code fails to account for the asymmetric i32 range. Established by the direct and shaped repros.
Severity. Low-to-medium: no in-repo callers today, but this is pub stdlib RNG API, and a program drawing many random ints will eventually hit the crash with no way for the caller to prevent it. It is a latent hard-abort in a general-purpose API.
Related PRs. None touch random.w.
Recommendation — champion, one character. Use wrapping negation: let positive = if v < 0: 0 -% v else: v (confirmed above to map INT32_MIN to INT32_MIN; positive % range/% 100 then stays well-defined and the distribution is unchanged for every other draw). Alternatively guard v == INT32_MIN explicitly. Note 0 -% INT32_MIN yields INT32_MIN (still negative), so if strict non-negativity of positive matters, mask instead: let positive = (v as i64).abs() ... or (v & 0x7FFFFFFF); for the % range/% 100 use here the wrapping form is sufficient because the sign washes out, but the reviewer should pick the spelling that keeps positive % range in [0, range) for all inputs (a negative positive would make lo + (positive % range) land below lo). Acceptance criteria:
range_i32 and chance never panic for any i32 draw, including INT32_MIN; a direct unit probe feeding INT32_MIN through both returns a value in range.
range_i32(lo, hi) result stays in [lo, hi) for the INT32_MIN draw (verify the chosen negation keeps positive % range non-negative).
A test/ regression covers the INT32_MIN input.
Blockers / not tested. I did not force next_i32 to actually emit INT32_MIN via a seed (2^-32 per draw); reachability rests on the xorshift period argument plus the report's oracle. The overflow itself is reproduced directly. Linux/Windows not exercised.
Fixed in #2614 (main 23dd9db): std.random's draws mask the sign bit instead of negating, so the minimum value no longer overflows in range_i32 and chance; range_isize added for indices.
Activity
ehartford commentedon Sep 16, 2026
Review: reproduced
Verdict. Established. Both
range_i32andchancecomputelet positive = if v < 0: 0 - v else: v, and0 - INT32_MINoverflows under checked subtraction, aborting the process withpanic: integer overflow: i32 subtraction out of range.next_i32can drawINT32_MIN, so the panic fires with probability 2^-32 per call. The report's wrapping-negation fix is correct.Tested.
main@ 5809ac1 (2026-09-14);lib/std/random.w:46-60unchanged since the reported revision 450733ewith v0.15.2.1built from 5809ac1 (staging worktree); a compiler built from 450733e with the nightly-20260903 seed. Same panicRepros (
with run), both compilers:print(0 - v)withvar v: i32 = -2147483648panic: integer overflow: i32 subtraction out of rangerange_i32shape withv = INT32_MIN,range_i32(_, 0, 10)v = -5(control)5print(0 -% v)withv = INT32_MIN(proposed fix)-2147483648, no panicSource pins (5809ac1).
range_i32atlib/std/random.w:50(let positive = if v < 0: 0 - v else: v),chanceat:58, same shape. Draw sourcenext_i32at:41-43:(xorshift64() >> 16) as i32.Reachability of the MIN draw.
next_i32truncates(xorshift64() >> 16)toi32. xorshift64 has full period over its nonzero 64-bit state, so the low 32 bits of(state >> 16)take every value in[0, 2^32), including0x80000000=INT32_MIN. So the input that overflows is genuinely producible (probability ~2^-32 per call). The report verified the same by oracle; I established it by the generator's structure rather than brute-forcing a 1-in-4-billion seed (see blockers).Root cause. Checked-arithmetic overflow on a reachable input;
0 - INT32_MINhas no representable i32 result. No compiler defect — the checked-is doing its job; the stdlib code fails to account for the asymmetric i32 range. Established by the direct and shaped repros.Severity. Low-to-medium: no in-repo callers today, but this is
pubstdlib RNG API, and a program drawing many random ints will eventually hit the crash with no way for the caller to prevent it. It is a latent hard-abort in a general-purpose API.Related PRs. None touch
random.w.Recommendation — champion, one character. Use wrapping negation:
let positive = if v < 0: 0 -% v else: v(confirmed above to mapINT32_MINtoINT32_MIN;positive % range/% 100then stays well-defined and the distribution is unchanged for every other draw). Alternatively guardv == INT32_MINexplicitly. Note0 -% INT32_MINyieldsINT32_MIN(still negative), so if strict non-negativity ofpositivematters, mask instead:let positive = (v as i64).abs() ...or(v & 0x7FFFFFFF); for the% range/% 100use here the wrapping form is sufficient because the sign washes out, but the reviewer should pick the spelling that keepspositive % rangein[0, range)for all inputs (a negativepositivewould makelo + (positive % range)land belowlo). Acceptance criteria:range_i32andchancenever panic for anyi32draw, includingINT32_MIN; a direct unit probe feedingINT32_MINthrough both returns a value in range.range_i32(lo, hi)result stays in[lo, hi)for theINT32_MINdraw (verify the chosen negation keepspositive % rangenon-negative).test/regression covers theINT32_MINinput.Blockers / not tested. I did not force
next_i32to actually emitINT32_MINvia a seed (2^-32 per draw); reachability rests on the xorshift period argument plus the report's oracle. The overflow itself is reproduced directly. Linux/Windows not exercised.Stdlib sourcing close-out: Stack/Queue/LinkedList, stable_sort/shuffl…
ehartford commentedon Oct 24, 2026
Fixed in #2614 (main 23dd9db): std.random's draws mask the sign bit instead of negating, so the minimum value no longer overflows in range_i32 and chance; range_isize added for indices.