Repository navigation
fix(primitives): return Err on parse_units multiply overflow - #1156
Merged
Merged
Conversation
`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]>
DaniPopes
approved these changes
Jul 20, 2026
DaniPopes
enabled auto-merge (squash)
July 20, 2026 20:49
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
ParseUnits::parse_unitsreturns aResultand is documented/tested to reject out-of-range inputs with anErr— see the existingassert!(parse_units("1", 80).is_err(), "overflow")test. It correctly guards the10^exponentfactor withchecked_pow, but then folds that factor into the parsed mantissa with an unchecked*=:When
10^exponentfits in the target integer (sochecked_powsucceeds) butmantissa * 10^exponentdoes not, the overflow is silently swallowed:U256: ruint implementsMul/MulAssignviawrapping_mul, so the multiply wraps to a wrong value in every build profile. For exampleparse_units("2", 77)—10^77 < U256::MAX < 2·10^77— returnsOk(<wrapped garbage>)instead ofErr.I256:Signed::mulusesdebug_assert!(!overflow), so the equivalent input panics in debug builds and silently wraps in release. For exampleparse_units("-6", 76)(10^76 < I256::MAX < 6·10^76).Both outcomes contradict the intended contract (graceful
Erron overflow).Solution
Replace the unchecked
*=withchecked_mulon both the unsigned and signed paths, propagating the sameUnitsError::ParseSigned(ParseSignedError::IntegerOverflow)already used for thechecked_powguard. 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 returnsOk; signed panics in debug) and passes after.Testing
cargo test -p alloy-primitives --lib utils::units— all pass.cargo +nightly fmt --checkclean.