Skip to content

fix(primitives): return Err on parse_units multiply overflow - #1156

Merged
DaniPopes merged 1 commit into
alloy-rs:mainfrom
Kropiunig:fix/parse-units-mul-overflow
Jul 20, 2026
Merged

DaniPopes merged 1 commit into
alloy-rs:mainfrom
Kropiunig:fix/parse-units-mul-overflow

Conversation

@Kropiunig

Copy link
Copy Markdown
Contributor

Motivation

ParseUnits::parse_units returns a Result and is documented/tested to reject out-of-range inputs with an Err — see the existing assert!(parse_units("1", 80).is_err(), "overflow") test. It correctly guards the 10^exponent factor with checked_pow, but then folds that factor into the parsed mantissa with an unchecked *=:

let mut a_uint = U256::from_str_radix(amount, 10)?;
a_uint *= U256::from(10)
    .checked_pow(U256::from(exponent - dec_len))
    .ok_or(UnitsError::ParseSigned(ParseSignedError::IntegerOverflow))?;

When 10^exponent fits in the target integer (so checked_pow succeeds) but mantissa * 10^exponent does not, the overflow is silently swallowed:

  • Unsigned U256: ruint implements Mul/MulAssign via wrapping_mul, so the multiply wraps to a wrong value in every build profile. For example parse_units("2", 77) — 10^77 < U256::MAX < 2·10^77 — returns Ok(<wrapped garbage>) instead of Err.
  • Signed I256: Signed::mul uses debug_assert!(!overflow), so the equivalent input panics in debug builds and silently wraps in release. For example parse_units("-6", 76) (10^76 < I256::MAX < 6·10^76).

Both outcomes contradict the intended contract (graceful Err on overflow).

Solution

Replace the unchecked *= with checked_mul on both the unsigned and signed paths, propagating the same UnitsError::ParseSigned(ParseSignedError::IntegerOverflow) already used for the checked_pow guard. Minimal change, no new error variants.

Adds a regression test (test_parse_units_mul_overflow) covering the unsigned wrap and signed overflow paths. It fails before this change (unsigned returns Ok; signed panics in debug) and passes after.

Testing

cargo test -p alloy-primitives --lib utils::units — all pass. cargo +nightly fmt --check clean.

`ParseUnits::parse_units` guards `10^exponent` with `checked_pow` but then
multiplies it into the parsed mantissa with an unchecked `*=`. When
`10^exponent` fits in the target integer but `mantissa * 10^exponent` does
not, this overflow is not caught:

- unsigned `U256`: ruint's `Mul`/`MulAssign` is `wrapping_mul`, so the result
  silently wraps to a wrong value in every profile (e.g. `parse_units("2", 77)`
  returns `Ok` with a garbage value instead of an error);
- signed `I256`: `Signed::mul` uses `debug_assert!(!overflow)`, so it panics in
  debug builds and silently wraps in release.

Both contradict the documented behavior for out-of-range inputs, which is a
graceful `Err` (as asserted by the existing `parse_units("1", 80).is_err()`
test). Replace the unchecked `*=` with `checked_mul`, propagating the same
`IntegerOverflow` error already used for the `checked_pow` guard.

Adds a regression test covering the unsigned wrap and signed overflow paths.

Signed-off-by: Kropiunig <[email protected]>
@github-project-automation github-project-automation Bot moved this to Reviewed in Alloy Jul 20, 2026
@DaniPopes
DaniPopes enabled auto-merge (squash) July 20, 2026 20:49
@DaniPopes
DaniPopes merged commit a5df2f9 into alloy-rs:main Jul 20, 2026
31 checks passed
@github-project-automation github-project-automation Bot moved this from Reviewed to Done in Alloy Jul 20, 2026
sveitser added a commit to EspressoSystems/espresso-network that referenced this pull request Sep 8, 2026
alloy-core 1.7 returns an error on `parse_units` multiply overflow instead
of wrapping (alloy-rs/core#1156), so `--amount U256::MAX` is now rejected by
clap before any transaction and the test never reached the ERC20 revert.

--amount is denominated in ESP, so use an amount above the 3.59e9 ESP test
supply. Adds a case pinning the new rejection behaviour.
sveitser added a commit to EspressoSystems/espresso-network that referenced this pull request Sep 8, 2026
* fix(contracts): keep bytecode in artifacts and bindings

Foundry 1.8 made `extra_output` replace the default output selection
instead of extending it, so `extra_output = ["storageLayout"]` dropped
`evm.bytecode` from the artifacts and the generated bindings lost their
`BYTECODE`/`DEPLOYED_BYTECODE` constants.

`forge bind` also builds with its own reduced output selection, so
gen-bindings now builds first and binds with --skip-build.

* chore(contracts): regenerate bindings with foundry 1.8

The new codegen uses `AbiDecoderConfig`, which requires alloy-core 1.7,
so bump the locked alloy-core crates from 1.6.0 to 1.7.2.

It also derives PartialEq on event structs, making the hand-written impls
in sol_types.rs conflicting duplicates; they compared every field, so the
derives are equivalent.

Also applies `forge fmt` to StakeTable.t.sol.

* revert(contracts): drop redundant extra_output bytecode entries

`extra_output` extends the default output selection rather than replacing
it, so `forge build` already emits bytecode without these entries.

The missing bytecode came solely from `forge bind` compiling an ABI-only
project (`compile_abi_project` overwrites the output selection), which the
`--skip-build` change in 863e278 already fixes.

* refactor(contracts): trim no-op build flags from forge bind

With --skip-build, bind does not compile, so --skip, --use and
--contracts have no effect. Dropping them makes it clear the artifacts
come from the forge build above and this step is only codegen.

Regenerated bindings are byte-identical.

* chore(contracts): remove dead serde_arrays binding hack

The sed inserted #[serde(with = "serde_arrays")] before a
`pub proof: [FixedBytes<32>; 160usize]` binding field. RewardClaim's
external interface takes `bytes authData` and decodes the bytes32[160]
into a local (RewardClaim.sol:349), so that field is not in the ABI and
never appeared in the generated bindings on main. serde_arrays was
declared but never referenced by any crate.

* chore(contracts): silence new foundry 1.8 lints

foundry 1.8 enabled rules that report 54 warnings and 1010 notes across
contracts/src/, failing CI's `forge lint --deny notes`. No contract logic
changes; bytecode is unchanged and `just gen-bindings` is a no-op.

- foundry.toml: exclude 10 style rules repo-wide, 1010 notes, dominated by
  literal-instead-of-constant (859) and inline-assembly (102).
- The 54 warnings are suppressed inline, each with the reason it is safe.
  unsafe-typecast is left bare; the cast is its own explanation.
- costly-loop is scoped to the one loop it fires in rather than excluded.

Only genuine finding: EspTokenV2.initializeV2 sets the mint authority with
no event. Write-once in reinitializer(2), owner-gated, readable via
rewardClaim(); accepted as-is and recorded in the comment.

* test(staking-cli): fix transfer amount overflowing the ether parser

alloy-core 1.7 returns an error on `parse_units` multiply overflow instead
of wrapping (alloy-rs/core#1156), so `--amount U256::MAX` is now rejected by
clap before any transaction and the test never reached the ERC20 revert.

--amount is denominated in ESP, so use an amount above the 3.59e9 ESP test
supply. Adds a case pinning the new rejection behaviour.

* test(staking-cli): drop comment and overflow case from transfer test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants