Repository navigation
Match Node's odd-length, ill-formed input and error handling in node:buffer transcode() - #8711
Conversation
There was a problem hiding this comment.
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
f74c8cc to
440f84b
Compare
There was a problem hiding this comment.
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
InvalidCharmessage inerror.rsmatches the message the TypeScript wrapper builds forU_INVALID_CHAR_FOUND, anderror-test.rsnow 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
440f84b to
33cb39f
Compare
There was a problem hiding this comment.
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.
TranscodeExceptionininternal_errors.tsis a plainError. It takescodeanderrnofrom a typed map (U_ILLEGAL_ARGUMENT_ERROR= 1,U_INVALID_CHAR_FOUND= 10), and the tests check what it produces. - Matching message.
kInvalidCharMessageininternal_buffer.tsmatchesTranscodeError::InvalidCharinerror.rs, anderror-test.rschecks 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
`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.
33cb39f to
83a69d9
Compare
There was a problem hiding this comment.
✅ 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 == 0check in the UTF-16LE to UTF-8 path cannot fire on an empty source, because a zero-length destination returnsOk(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:
kInvalidCharMessagematchesTranscodeError::InvalidChar, anderror-test.rschecks that exact string. - Error helper:
TranscodeExceptionlives ininternal_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
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: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
codeanderrno.hexand 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:
U_INVALID_CHAR_FOUNDError, withcodeanderrno. 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'sU_ILLEGAL_ARGUMENT_ERRORError 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.