Skip to content

Signed PHIR register size of usize::MAX overflows the width check (debug panic, release accepts) #1341

Open

Description

@ciaranra

A signed classical register declared with size equal to usize::MAX overflows the register-width check.

Reproduction

src = {"format": "PHIR/JSON", "version": "0.1.0", "ops": [
    {"data": "cvar_define", "data_type": "i64", "variable": "a", "size": 18446744073709551615},
    {"cop": "=", "args": [5], "returns": ["a"]},
]}
pecos_rslib.run_phir_sim(json.dumps(src))

In a debug build of pecos_rslib, this panics: attempt to add with overflow at crates/pecos-phir-json/src/v0_1/environment.rs:617 (#1314 branch).

Cause

Environment::validate_variable_size (crates/pecos-phir-json/src/v0_1/environment.rs, ~589 on dev) computes needed = size + 1 for signed types. For size == usize::MAX:

  • debug builds panic;
  • release builds (including the Python wheel) wrap needed to 0, so the check passes and the declaration is accepted, although the size cannot fit any backing type.

The Python interpreter rejects the same declaration, since Python integers do not overflow, so the paths disagree.

Direction

Use size.checked_add(1) and treat None as "does not fit". The existing "use at most N bits" error then covers it. Add a test with size: usize::MAX for a signed type on native, the Rust interpreter and the JSON-to-module converter.

Found during the review of the #1314 work.

Activity

  1. added
    bugSomething isn't working
    rustPull requests that update rust code
    severity:lowCosmetic, docs, or rare edge case
    on Oct 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

    bugSomething isn't workingrustPull requests that update rust codeseverity:lowCosmetic, docs, or rare edge case

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions