Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d172669

Browse filesBrowse files
cola119RafaelGSS
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 d5f4d98 commit d172669
Copy full SHA for d172669

File tree

Expand file treeCollapse file tree

3 files changed

+26
-10
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+26
-10
lines changed
Open diff view settings
Collapse file

‎lib/internal/encoding.js‎

Copy file name to clipboardExpand all lines: lib/internal/encoding.js
+18-4Lines changed: 18 additions & 4 deletions
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'],
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-whatwg-encoding-custom-textdecoder.js
+7Lines changed: 7 additions & 0 deletions
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+
}
Collapse file

‎test/wpt/status/encoding.json‎

Copy file name to clipboardExpand all lines: test/wpt/status/encoding.json
+1-6Lines changed: 1 addition & 6 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.