Skip to content

Commit

Permalink
lib: fix TypeError when converting a detached buffer source
Browse files Browse the repository at this point in the history
  • Loading branch information
cola119 committed Jul 28, 2022
1 parent abddacb commit bbb15af
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 7 additions & 1 deletion 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
7 changes: 7 additions & 0 deletions test/parallel/test-whatwg-encoding-custom-textdecoder.js
Expand Up @@ -199,3 +199,10 @@ if (common.hasIntl) {
const str = decoder.decode(chunk);
assert.strictEqual(str, 'foo\ufffd');
}

{
const buffer = new ArrayBuffer(1);
new MessageChannel().port1.postMessage(buffer, [buffer]); // buffer is detached
const decoder = new TextDecoder();
decoder.decode(buffer);
}

0 comments on commit bbb15af

Please sign in to comment.