lib: fix TypeError when converting a detached buffer source

PR-URL: https://github.com/nodejs/node/pull/44020
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
Kohei Ueno authored and GitHub committed 2022-11-01 23:59:26 +00:00
1 parent f1e93820a7
commit 86088ab78e
3 files changed
+26 -10

No files matched your search

+18 -4
View File
@@ -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'],
@@ -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'],
@@ -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), '');
}
+1 -6
View File
@@ -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": {