Skip to content

Match Node's odd-length, ill-formed input and error handling in node:buffer transcode() - #8711

Merged
littledivy merged 2 commits into
mainfrom
littledivy/2026-11-20-transcode-divergences
Nov 21, 2026
Merged

littledivy merged 2 commits into
mainfrom
littledivy/2026-11-20-transcode-divergences

Conversation

@littledivy

Copy link
Copy Markdown
Member

After #8687, transcode() still differed from Node 26 in how it handles odd-length UTF-16LE, ill-formed UTF-8 and UTF-16LE, and bad arguments:

transcode(Buffer.from([0x41, 0x00, 0x42]), 'ucs2', 'utf8');
// Node: 'A'                     workerd: throws "UTF-16le input size should be multiple of 2"
transcode(Buffer.from([0x80]), 'utf8', 'ucs2');
// Node: throws U_INVALID_CHAR_FOUND   workerd: empty buffer
transcode(Buffer.from('a'), 'utf8', 'foo');
// Node: Error, code 'U_ILLEGAL_ARGUMENT_ERROR', errno 1   workerd: TypeError ERR_UNKNOWN_ENCODING
transcode(null, 'utf8', 'ucs2');
// Node: "... must be an instance of Buffer or Uint8Array. Received null"
// workerd: "... must be an instance of ArrayBufferView. Received type string ('object')"

Other ill-formed input to the UTF-8/UTF-16LE pairs failed with "Unable to transcode buffer" or "Expected UTF8 length mismatch", without Node's code and errno. hex and the other encodings that can't be transcoded failed with a message of their own, and an empty source with a bad encoding threw where Node returns an empty Buffer.

In this patch:

  • A UTF-16LE source into UTF-8, ASCII or Latin-1 ignores its trailing odd byte, as Node's test-icu-transcode.js checks, and a lone byte into UTF-8 fails, as in Node. The first commit drops the test that pinned the throw.
  • Ill-formed input to the UTF-8/UTF-16LE pairs fails as Node's U_INVALID_CHAR_FOUND Error, with code and errno. That includes UTF-8 made only of continuation bytes, which returned an empty buffer before.
  • transcode() checks its arguments in Node's order: only a Uint8Array, an empty Buffer for an empty source, then Node's U_ILLEGAL_ARGUMENT_ERROR Error for an unknown or untranscodable encoding.

I compared workerd with Node 26 using the two runs from #8687: 2706 hand-picked calls and 4000 seeded random ones. Before, 443 hand-picked calls gave a different result or error. Now every call gives the same bytes, or the same error name, code, errno and message.

Two changes could be seen by existing code. UTF-8 made only of continuation bytes into UTF-16LE now throws instead of returning an empty buffer, and unknown encoding names throw an Error instead of a TypeError. We accept both without a compat flag, since the old results don't match Node.

@littledivy
littledivy requested review from a team as code owners November 20, 2026 05:02

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 440f84b4.

This review, of f74c8cc0

✅ Approved — No defects found. The ICU error mapping, the odd-length UTF-16LE handling and the order of argument checks are consistent and covered by tests.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security concerns.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md concerns.


📋 Code Quality: ✅ LGTM

No problems found in the transcode error mapping or the odd-length handling. Each behaviour change has Rust and Node tests.


⚡ Performance: ✅ LGTM

No performance concerns.


📝 Documentation: ✅ LGTM

No documentation issues.


Reviewed commit: f74c8cc0

@littledivy
littledivy force-pushed the littledivy/2026-11-20-transcode-divergences branch from f74c8cc to 440f84b Compare November 20, 2026 05:06

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 33cb39f6.

This review, of 440f84b4

✅ Approved — No defects found. The ICU error mapping, the odd-length UTF-16LE handling and the order of the argument checks all match Node and have tests. The only change since the last review makes a test stricter.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security concerns.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md concerns.


📋 Code Quality: ✅ LGTM

No problems found:

  • Error message. The InvalidChar message in error.rs matches the message the TypeScript wrapper builds for U_INVALID_CHAR_FOUND, and error-test.rs now checks it exactly.
  • Removed code. Nothing still refers to OddUtf16leInput, and no imports are left unused.
  • Odd-length and empty input. Odd-length UTF-16LE and empty sources are handled the way the pull request describes.

⚡ Performance: ✅ LGTM

No performance concerns.


📝 Documentation: ✅ LGTM

No documentation issues.


Reviewed commit: 440f84b4

Comment thread src/node/internal/internal_buffer.ts Resolved
@littledivy
littledivy force-pushed the littledivy/2026-11-20-transcode-divergences branch from 440f84b to 33cb39f Compare November 20, 2026 17:05

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 83a69d97.

This review, of 33cb39f6

✅ Approved — No defects found. The new TranscodeException matches Node's error shape and is tested, and the earlier review's assessment still holds.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security concerns.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md concerns.


📋 Code Quality: ✅ LGTM

No problems found:

  • TranscodeException. TranscodeException in internal_errors.ts is a plain Error. It takes code and errno from a typed map (U_ILLEGAL_ARGUMENT_ERROR = 1, U_INVALID_CHAR_FOUND = 10), and the tests check what it produces.
  • Matching message. kInvalidCharMessage in internal_buffer.ts matches TranscodeError::InvalidChar in error.rs, and error-test.rs checks that exact message.
  • Argument checks. The checks run in Node's order: a source that is not a Uint8Array is rejected, an empty source returns an empty Buffer, and an unknown or untranscodable encoding throws U_ILLEGAL_ARGUMENT_ERROR.
  • Rust changes. They agree with the tests, and nothing still refers to OddUtf16leInput.

⚡ Performance: ✅ LGTM

No performance concerns.


📝 Documentation: ✅ LGTM

No documentation issues.


Reviewed commit: 33cb39f6

@littledivy
littledivy enabled auto-merge November 20, 2026 17:45
`transcode()` threw for a UTF-16LE source of odd length, where Node converts the whole code units and ignores the trailing byte. Node's own test-icu-transcode.js checks this:

```js
transcode(Buffer.from([0x41, 0x00, 0x42]), 'ucs2', 'utf8');  // Node: 'A', workerd: throws
```

Ill-formed input to the UTF-8/UTF-16LE pairs failed with "Unable to transcode buffer" or "Expected UTF8 length mismatch", where Node fails with "Unable to transcode Buffer [U_INVALID_CHAR_FOUND]". UTF-8 made only of continuation bytes didn't fail at all: simdutf sizes it at zero UTF-16 code units, so `transcode(Buffer.from([0x80]), 'utf8', 'ucs2')` returned an empty buffer.

In this patch a UTF-16LE source into UTF-8, ASCII or Latin-1 drops its trailing odd byte, and a lone byte into UTF-8 fails, as in Node. The UTF-16LE identity pair is unchanged, since it already writes U+FFFD for the odd byte, as in Node. Ill-formed input to the UTF-8/UTF-16LE pairs, including UTF-8 made only of continuation bytes, fails with Node's message.

Continuation-only UTF-8 into UTF-16LE now throws instead of returning an empty buffer, as every other ill-formed UTF-8 on that pair already did. We accept that without a compat flag, along with the new message: the empty result was wrong, and code written for Node expects the throw.
`transcode()` reported bad arguments and failed conversions differently from Node, which code that branches on `err.code` notices:

```js
transcode(Buffer.from('a'), 'utf8', 'foo');
// Node: Error, code 'U_ILLEGAL_ARGUMENT_ERROR', errno 1
// workerd: TypeError, code 'ERR_UNKNOWN_ENCODING'
transcode(Buffer.from([0x80]), 'utf8', 'ucs2');
// Node: Error, code 'U_INVALID_CHAR_FOUND', errno 10
// workerd: Error, no code
transcode(null, 'utf8', 'ucs2');
// Node: "... must be an instance of Buffer or Uint8Array. Received null"
// workerd: "... must be an instance of ArrayBufferView. Received type string ('object')"
```

Encodings that can't be transcoded, like `hex`, failed with "Unable to transcode buffer due to unsupported encoding". The source check accepted any ArrayBufferView, so a DataView got as far as the native binding and failed there with a generic TypeError. An empty source with a bad encoding threw, where Node returns an empty Buffer before it looks at the encodings.

In this patch `transcode()` checks its arguments in Node's order. It takes only a Uint8Array, returns an empty Buffer for an empty source, and reports an unknown or untranscodable encoding as Node's `U_ILLEGAL_ARGUMENT_ERROR` Error. The native error for ill-formed input gains Node's `code` and `errno`.

Unknown encoding names now throw an Error rather than a TypeError, and the other errors change only in their message or gain `code` and `errno`. We accept that without a compat flag, since the errors match Node's.
@littledivy
littledivy force-pushed the littledivy/2026-11-20-transcode-divergences branch from 33cb39f to 83a69d9 Compare November 21, 2026 02:48

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved — No defects found. The Rust changes since the last review (odd-length UTF-16LE handling, InvalidChar for continuation-only UTF-8) match the tests, and the earlier assessment still holds.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security concerns.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md concerns.


📋 Code Quality: ✅ LGTM

No problems in dispatch.rs, dispatch-test.rs or error.rs, the files changed since the last review:

  • Empty source: The new written == 0 check in the UTF-16LE to UTF-8 path cannot fire on an empty source, because a zero-length destination returns Ok(0) before it gets there.
  • Odd-length UTF-16LE: The trailing odd byte is dropped the same way in every path. A lone byte still fails early, and the Latin-1/ASCII arm truncates with len & !1.
  • UTF-8 to UTF-16LE: The guard for a zero estimate on non-empty input catches UTF-8 made only of continuation bytes before the size check runs.
  • Shared message: kInvalidCharMessage matches TranscodeError::InvalidChar, and error-test.rs checks that exact string.
  • Error helper: TranscodeException lives in internal_errors.ts, as @​jasnell asked on the earlier thread.
  • Compatibility: The commit messages record that both visible behaviour changes are accepted without a compat flag.

⚡ Performance: ✅ LGTM

No performance concerns. The new checks take constant time. They run next to conversions that were already linear, and an empty source now returns without calling into native code.


📝 Documentation: ✅ LGTM

No documentation issues.


Reviewed commit: 83a69d97

@littledivy
littledivy merged commit aa3f632 into main Nov 21, 2026
35 of 37 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-20-transcode-divergences branch November 21, 2026 03:13
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