Skip to content

fix(Hex): throw on empty hex in toNumber instead of returning NaN#275

Open
spokodev wants to merge 1 commit into
wevm:mainfrom
spokodev:fix/hex-tonumber-empty-hex
Open

fix(Hex): throw on empty hex in toNumber instead of returning NaN#275
spokodev wants to merge 1 commit into
wevm:mainfrom
spokodev:fix/hex-tonumber-empty-hex

Conversation

@spokodev

Copy link
Copy Markdown

Hex.toNumber('0x') returns NaN, while Hex.toBigInt('0x') throws.

The no-options fast path decodes via Number(hex), and Number('0x') is NaN, so empty hex silently yields a non-number instead of erroring. viem's hexToNumber routes through hexToBigInt and throws here; Bytes.toNumber (which delegates to Hex.toNumber) is affected too.

The fix keeps the fast path for valid hex and only defers to toBigInt when Number(hex) is NaN. For all valid hex, Number(hex) === Number(toBigInt(hex)); 0x is the only divergence, so no valid result changes.

Added a regression test asserting Hex.toNumber('0x') throws.

@vercel

vercel Bot commented Jun 22, 2026

Copy link
Copy Markdown

@spokodev is attempting to deploy a commit to the Wevm Team on Vercel.

A member of the Team first needs to authorize it.

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.

1 participant