Repository navigation
Fix CompactSize.toBytes silently truncating values above 2^64 - 1 - #503
Merged
Merged
Conversation
`DataView#setBigUint64` reduces its argument modulo 2^64, so `CompactSize.toBytes` silently wrapped values above 2^64 - 1 into a different 9-byte encoding (e.g. 2^64 + 2^32 encoded as 2^32). Throw `CompactSize.ValueTooLargeError` instead, mirroring the existing `NegativeValueError` lower-bound check. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@ara-stock is attempting to deploy a commit to the Wevm Team on Vercel. A member of the Team first needs to authorize it. |
Merged
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.
CompactSize.toBytes(andtoHex, which goes through it) checks the lower bound (NegativeValueError) but not the upper bound. For values above2^64 - 1, the 9-byte branch callsDataView#setBigUint64, which reduces its argument modulo 2^64 and does not throw. You get the encoding of a different number back, with no error:This PR adds an upper-bound check that throws a new
CompactSize.ValueTooLargeError, which has the same shape asNegativeValueError.2^64 - 1still encodes as before.Before / after (
toHex→fromHex):2^64 - 10xffffffffffffffffff→2^64 - 12^640xff0000000000000000→NonMinimalEncodingErrorValueTooLargeError2^64 + 2^320xff0000000001000000→4294967296nValueTooLargeErrorAlternatives considered: reusing
InvalidValueError. I didn't, because its message and parameter are specific to non-safe-integer numbers.The same code is on
0.x, if a backport is wanted.Tests: added
toBytes > error: value larger than uint64. It fails onmain("snapshot function didn't throw") and passes with the fix.pnpm test --project core src/core/_test/CompactSize.test.ts(16 passed),vp checkon the touched files, andpnpm check:typesall pass. A patch changeset is included.I used an AI assistant (Claude) while preparing this. I reproduced the issue and checked the fix and tests myself.