Repository navigation
fix(abi): do not trim leading zero bytes when decoding strings - #4844
Merged
Merged
Conversation
The dynamic `string` decoder in `decodeAbiParameters` read the payload with `cursor.readBytes(length, 32)` (exactly `length` UTF-8 bytes, no left padding) and then passed it through `trim(data)`. `trim` defaults to `dir: 'left'` and strips leading `0x00` bytes, so any decoded string whose UTF-8 encoding begins with one or more NUL bytes was silently corrupted: the leading NUL byte(s) were dropped with no error. For example, decoding the ABI encoding of `"\x00ab"` returned `"ab"`, and `"\x00\x00x"` returned `"x"`. This propagated through `decodeFunctionResult`, `readContract`, event/error decoding, etc. Dynamic `bytes` (`decodeBytes`) does not trim, so `string` and `bytes` decoded inconsistently for identical payloads. The ABI length prefix is authoritative, so the payload is now decoded as-is via `bytesToString(data)`, preserving leading NUL bytes and matching `bytes` behavior. Adds a round-trip and a hand-crafted regression test.
|
@Kropiunig is attempting to deploy a commit to the Wevm Team on Vercel. A member of the Team first needs to authorize it. |
🦋 Changeset detectedLatest commit: 5c79b17 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
commit: |
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.
Summary
The dynamic
stringdecoder indecodeAbiParameterssilently corrupts any decodedstringwhose UTF-8 encoding begins with one or more NUL (0x00) bytes.In
decodeString(src/utils/abi/decodeAbiParameters.ts), the payload is read with:trimdefaults todir: 'left', so it strips leading0x00bytes fromdata. Since the ABI length prefix is authoritative and the payload has no left padding, this trim is unnecessary and drops the leading NUL byte(s) with no error. The corruption propagates throughdecodeFunctionResult,readContract, event/error decoding, etc.Dynamic
bytes(decodeBytes, same file) does not trim, sostringandbytesdecode inconsistently for identical payloads.Failing scenario
Decoding the ABI encoding of the
stringvalue"\x00ab"(payload bytes00 61 62; head/tail = offset0x20, length3, data word0061620000…) returns"ab"instead of"\x00ab". Likewise"\x00\x00x"decodes to"x". Round-trippingencodeAbiParameters->decodeAbiParametersfails for these leading-NUL strings.Interior NULs (
"a\x00b"), a lone"\x00", and all normal ASCII strings are unaffected, which is why no existing test vector caught this.Fix
Decode the payload as-is; the length prefix already delimits it exactly:
This preserves leading NUL bytes and makes
stringdecoding consistent withbytes. The now-unused runtimetrimimport is dropped (theTrimErrorTypetype is still referenced byDecodeStringErrorType).Tests
Adds a regression test in
decodeAbiParameters.test.ts: a round-trip over["\x00ab", "\x00\x00x", "a\x00b", "\x00"]and a hand-crafted hex decode asserting"\x00ab". Both fail on the current code and pass with the fix; the existing ASCII string tests are unchanged.