fix(primitives): return Err on parse_units multiply overflow - #1156
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 <48442031+Kropiunig@users.noreply.github.com>
DaniPopes
approved these changes
Jul 20, 2026
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.