Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: fix TypeError when converting a detached buffer source #44020

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 18 additions & 4 deletions lib/internal/encoding.js
Expand Up @@ -412,7 +412,13 @@ function makeTextDecoderICU() {
decode(input = empty, options = kEmptyObject) {
validateDecoder(this);
if (isAnyArrayBuffer(input)) {
input = lazyBuffer().from(input);
try {
input = lazyBuffer().from(input);
} catch {
// If the buffer is detached,
// use an empty Uint8Array to avoid TypeError
input = empty;
}
} else if (!isArrayBufferView(input)) {
throw new ERR_INVALID_ARG_TYPE('input',
['ArrayBuffer', 'ArrayBufferView'],
Expand Down Expand Up @@ -485,10 +491,18 @@ function makeTextDecoderJS() {
decode(input = empty, options = kEmptyObject) {
validateDecoder(this);
if (isAnyArrayBuffer(input)) {
input = lazyBuffer().from(input);
try {
input = lazyBuffer().from(input);
} catch {
input = empty;
}
} else if (isArrayBufferView(input)) {
input = lazyBuffer().from(input.buffer, input.byteOffset,
input.byteLength);
try {
input = lazyBuffer().from(input.buffer, input.byteOffset,
input.byteLength);
} catch {
input = empty;
}
} else {
throw new ERR_INVALID_ARG_TYPE('input',
['ArrayBuffer', 'ArrayBufferView'],
Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-whatwg-encoding-custom-textdecoder.js
Expand Up @@ -211,3 +211,10 @@ if (common.hasIntl) {
assert.strictEqual(e.code, 'ERR_ENCODING_NOT_SUPPORTED');
}
}

{
const buffer = new ArrayBuffer(1);
new MessageChannel().port1.postMessage(buffer, [buffer]); // buffer is detached
const decoder = new TextDecoder();
assert.strictEqual(decoder.decode(buffer), '');
}
7 changes: 1 addition & 6 deletions test/wpt/status/encoding.json
Expand Up @@ -61,12 +61,7 @@
"requires": ["small-icu"]
},
"streams/decode-utf8.any.js": {
"requires": ["small-icu"],
"fail": {
"expected": [
"decoding a transferred ArrayBuffer chunk should give no output"
]
}
"requires": ["small-icu"]
},
"streams/decode-bad-chunks.any.js": {
"fail": {
Expand Down