Skip to content

fix(abi): do not trim leading zero bytes when decoding strings - #4844

Merged
jxom merged 2 commits into
wevm:mainfrom
Kropiunig:fix/decode-string-leading-zero-bytes
Jul 16, 2026
Merged

jxom merged 2 commits into
wevm:mainfrom
Kropiunig:fix/decode-string-leading-zero-bytes

Conversation

@Kropiunig

Copy link
Copy Markdown
Contributor

Summary

The dynamic string decoder in decodeAbiParameters silently corrupts any decoded string whose UTF-8 encoding begins with one or more NUL (0x00) bytes.

In decodeString (src/utils/abi/decodeAbiParameters.ts), the payload is read with:

const data = cursor.readBytes(length, 32)   // exactly `length` UTF-8 bytes, no left padding
const value = bytesToString(trim(data))

trim defaults to dir: 'left', so it strips leading 0x00 bytes from data. 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 through decodeFunctionResult, readContract, event/error decoding, etc.

Dynamic bytes (decodeBytes, same file) does not trim, so string and bytes decode inconsistently for identical payloads.

Failing scenario

Decoding the ABI encoding of the string value "\x00ab" (payload bytes 00 61 62; head/tail = offset 0x20, length 3, data word 0061620000…) returns "ab" instead of "\x00ab". Likewise "\x00\x00x" decodes to "x". Round-tripping encodeAbiParameters -> decodeAbiParameters fails 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:

const value = bytesToString(data)

This preserves leading NUL bytes and makes string decoding consistent with bytes. The now-unused runtime trim import is dropped (the TrimErrorType type is still referenced by DecodeStringErrorType).

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.

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.
@vercel

vercel Bot commented Jul 15, 2026

Copy link
Copy Markdown

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

A member of the Team first needs to authorize it.

@changeset-bot

changeset-bot Bot commented Jul 15, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 5c79b17

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
viem Patch

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

@pkg-pr-new

pkg-pr-new Bot commented Jul 15, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/viem@4844

commit: e05311a

@jxom
jxom merged commit 1f4d4db into wevm:main Jul 16, 2026
9 of 26 checks passed
@github-actions github-actions Bot mentioned this pull request Jul 16, 2026
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.

2 participants