Skip to content

Commit 68690ef

Browse files
cola119danielleadams
authored andcommitted
lib: fix TypeError when converting a detached buffer source
PR-URL: #44020 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 8475b69 commit 68690ef

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

lib/internal/encoding.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,13 @@ function makeTextDecoderICU() {
412412
decode(input = empty, options = kEmptyObject) {
413413
validateDecoder(this);
414414
if (isAnyArrayBuffer(input)) {
415-
input = lazyBuffer().from(input);
415+
try {
416+
input = lazyBuffer().from(input);
417+
} catch {
418+
// If the buffer is detached,
419+
// use an empty Uint8Array to avoid TypeError
420+
input = empty;
421+
}
416422
} else if (!isArrayBufferView(input)) {
417423
throw new ERR_INVALID_ARG_TYPE('input',
418424
['ArrayBuffer', 'ArrayBufferView'],
@@ -485,10 +491,18 @@ function makeTextDecoderJS() {
485491
decode(input = empty, options = kEmptyObject) {
486492
validateDecoder(this);
487493
if (isAnyArrayBuffer(input)) {
488-
input = lazyBuffer().from(input);
494+
try {
495+
input = lazyBuffer().from(input);
496+
} catch {
497+
input = empty;
498+
}
489499
} else if (isArrayBufferView(input)) {
490-
input = lazyBuffer().from(input.buffer, input.byteOffset,
491-
input.byteLength);
500+
try {
501+
input = lazyBuffer().from(input.buffer, input.byteOffset,
502+
input.byteLength);
503+
} catch {
504+
input = empty;
505+
}
492506
} else {
493507
throw new ERR_INVALID_ARG_TYPE('input',
494508
['ArrayBuffer', 'ArrayBufferView'],

test/parallel/test-whatwg-encoding-custom-textdecoder.js

+7
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,10 @@ if (common.hasIntl) {
211211
assert.strictEqual(e.code, 'ERR_ENCODING_NOT_SUPPORTED');
212212
}
213213
}
214+
215+
{
216+
const buffer = new ArrayBuffer(1);
217+
new MessageChannel().port1.postMessage(buffer, [buffer]); // buffer is detached
218+
const decoder = new TextDecoder();
219+
assert.strictEqual(decoder.decode(buffer), '');
220+
}

test/wpt/status/encoding.json

+1-6
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,7 @@
6161
"requires": ["small-icu"]
6262
},
6363
"streams/decode-utf8.any.js": {
64-
"requires": ["small-icu"],
65-
"fail": {
66-
"expected": [
67-
"decoding a transferred ArrayBuffer chunk should give no output"
68-
]
69-
}
64+
"requires": ["small-icu"]
7065
},
7166
"streams/decode-bad-chunks.any.js": {
7267
"fail": {

0 commit comments

Comments
 (0)