Skip to content

fix(explorers): return ParseOverflow instead of wrapping or panicking in parse_units - #283

Merged
mablr merged 1 commit into
foundry-rs:mainfrom
ninglinLiu:fix/parse-units-overflow
Oct 27, 2026
Merged

mablr merged 1 commit into
foundry-rs:mainfrom
ninglinLiu:fix/parse-units-overflow

Conversation

@ninglinLiu

Copy link
Copy Markdown
Contributor

parse_units scales the parsed amount by 10^(units - decimals), but the final multiplication was unchecked:

  • on the unsigned path, U256 *= wraps silently. parse_units("115792089237316195423570985008687907853269984665640564039458", "ether") (one more than U256::MAX / 1e18) returned Ok(U256(415992086870360064)), which is about 0.4 ether instead of an error;
  • on the negative path, I256 *= panics with overflow for the same amount with a leading -.

Both branches now use checked_mul and return the existing ConversionError::ParseOverflow, which the function already returns when 10^exponent itself overflows. Amounts that fit are unchanged. I also dropped the unused _fi local in the negative branch.

Tests. Added parse_units_rejects_overflow. It checks that the overflowing amount is rejected on both the positive and negative paths, and that 1.5 ether and the largest whole-ether amounts still parse. cargo test -p foundry-block-explorers --lib passes (31 tests), and cargo +nightly fmt and cargo clippy -p foundry-block-explorers --all-targets are clean.

This change was prepared with the help of an AI coding assistant.

@mablr
mablr enabled auto-merge (squash) October 27, 2026 10:38
@mablr
mablr merged commit 04cbcc6 into foundry-rs:main Oct 27, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants