Skip to content

[stdlib] random.range_i32/chance panic on INT32_MIN draw (checked 0 - v overflow) #1058

Closed

Description

@eas4ai

Summary

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.

Evidence (executed at 450733e)

  • 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).

Activity

  1. ehartford commented on Sep 16, 2026

    @ehartford
    Contributor

    Review: reproduced

    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.

  2. ehartford commented on Oct 24, 2026

    @ehartford
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions