Skip to content

fix(calldata): reject unsafe integers and unpaired surrogates in encoder - #232

Open
feafel wants to merge 1 commit into
genlayerlabs:v2-devfrom
feafel:fix/calldata-encoder-validation
Open

feafel wants to merge 1 commit into
genlayerlabs:v2-devfrom
feafel:fix/calldata-encoder-validation

Conversation

@feafel

@feafel feafel commented Oct 11, 2026 •

Copy link
Copy Markdown

Summary

Fixes #229: calldata.encode silently accepted two JavaScript value classes that cannot be represented losslessly:

  1. Unsafe integer numbers — Number.isInteger() accepts values outside JavaScript's safe-integer range, then BigInt(data) encodes the already-rounded Number.
  2. Strings with unpaired UTF-16 surrogates — TextEncoder replaces an unpaired surrogate with U+FFFD instead of failing, so decode(encode(value)) !== value and distinct map keys can collapse to the same UTF-8 bytes.

Changes

  • src/abi/calldata/encoder.ts: reject number values for which Number.isSafeInteger(value) is false
  • src/abi/calldata/encoder.ts: reject unpaired UTF-16 surrogates in calldata string values and map keys
  • Added hasUnpairedSurrogates() helper function
  • Added 9 regression tests in tests/calldata-encoder.test.ts

Testing

  • All 93 tests pass (84 existing + 9 new)
  • npx vitest run — clean

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Calldata encoding now rejects unsafe JavaScript integers and strings or map keys containing invalid UTF-16 surrogate sequences. Use bigint for large integers.
    • Valid integers, Unicode text, and properly paired surrogate characters continue to be accepted.

calldata.encode silently accepted:
1. Unsafe integer numbers (outside Number.isSafeInteger range) — BigInt(data) encoded the already-rounded Number
2. Strings with unpaired UTF-16 surrogates — TextEncoder replaced them with U+FFFD, causing decode(encode(value)) !== value and map key collisions

Now rejects both with clear error messages, matching the GenVM Python reference encoder behavior.

Closes genlayerlabs#229
@github-actions
github-actions Bot changed the base branch from main to v2-dev October 11, 2026 01:29
@github-actions

Copy link
Copy Markdown

This PR targeted main, which is only the default/static branch.

I retargeted it to v2-dev, the active development branch. Pushes to v2-dev automatically fast-forward main.

@coderabbitai

coderabbitai Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 964e23e8-b470-47d2-9e45-257622fb9e1a

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR



  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

This branch has not been deployed

No deployments
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.

calldata.encode silently accepts lossy JavaScript values

2 participants