Skip to content

fix(dyn-abi): coerce JSON int values above i64::MAX for signed types - #1158

Merged
DaniPopes merged 1 commit into
alloy-rs:mainfrom
nikolauspschuetz:fix/eip712-coerce-int-u64-json
Jul 24, 2026
Merged

DaniPopes merged 1 commit into
alloy-rs:mainfrom
nikolauspschuetz:fix/eip712-coerce-int-u64-json

Conversation

@nikolauspschuetz

Copy link
Copy Markdown
Contributor

Motivation

eip712 JSON coercion reads signed integers only through serde_json's as_i64, while the sibling uint path uses as_u64. serde_json stores any integer literal in (i64::MAX, u64::MAX] as a u64, so a valid positive value for int72..int256 — e.g. 2^63 coerced to int128 — never reaches the bounds check and coerce_json fails with a TypeMismatch, even though:

  • the same value coerces fine as a JSON string (the FromStr path), and
  • uint accepts the same JSON number.

So int128 accepts 9223372036854775808 as a string but rejects the identical JSON number.

Solution

Add a u64 fallback in int() mirroring uint() — a u64 always fits positively in I256, so I256::try_from(num) cannot fail here. The bounds check (x.bits() <= n) is unchanged.

Verification

New regression test int_coerces_json_number_above_i64_max asserts the number form coerces identically to the string form for Int(128). It fails on main:

TypeMismatch { expected: "int128", actual: "9223372036854775808" }

and passes with the fix. cargo test -p alloy-dyn-abi --features eip712 eip712::coerce → 4 passed.

PR Checklist

  • Added Tests
  • Added Documentation — n/a
  • Breaking changes — none (widens accepted inputs only)

@nikolauspschuetz
nikolauspschuetz marked this pull request as ready for review July 22, 2026 21:45
eip712 coerce_json read signed integers only through serde_json's as_i64,
while the sibling uint path uses as_u64. serde_json stores any integer in
(i64::MAX, u64::MAX] as a u64, so a valid positive value for int72..int256
(e.g. 2^63 into int128) returned None and coerce_json failed, even though
the same value is accepted as a JSON string and accepted by uint as a number.
Add a u64 fallback mirroring uint, plus a regression test.
@nikolauspschuetz
nikolauspschuetz force-pushed the fix/eip712-coerce-int-u64-json branch from cb5ad14 to e7926dd Compare July 24, 2026 04:57
if let Some(num) = value.as_i64() {
return Some(I256::try_from(num).unwrap());
}
if let Some(num) = value.as_u64() {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rationale: serde_json stores any integer in (i64::MAX, u64::MAX] as a u64, so as_i64() above returns None for it. Without this as_u64 fallback a valid positive value for int72..int256 (e.g. 2^63 as int128) falls through to the string branch and is rejected — even though the sibling uint() accepts the same JSON number. A u64 always fits positively in I256, so the try_from is infallible here.

@github-project-automation github-project-automation Bot moved this to Reviewed in Alloy Jul 24, 2026
@DaniPopes
DaniPopes enabled auto-merge (squash) July 24, 2026 21:00
@DaniPopes
DaniPopes merged commit a038aa8 into alloy-rs:main Jul 24, 2026
31 checks passed
@github-project-automation github-project-automation Bot moved this from Reviewed to Done in Alloy Jul 24, 2026
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