diff options
author | Kohei Ueno <kohei.ueno119@gmail.com> | 2022-11-02 02:59:26 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-02 02:59:26 +0300 |
commit | 86088ab78e8c3739f60627ddf21f65da7796450e (patch) | |
tree | f64c0c76a4ba37ef60719d5e24aa08d589ddd9ad | |
parent | f1e93820a7de77e9aec1a490b735f1fe42257da8 (diff) |
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>
-rw-r--r-- | lib/internal/encoding.js | 22 | ||||
-rw-r--r-- | test/parallel/test-whatwg-encoding-custom-textdecoder.js | 7 | ||||
-rw-r--r-- | test/wpt/status/encoding.json | 7 |
3 files changed, 26 insertions, 10 deletions
diff --git a/lib/internal/encoding.js b/lib/internal/encoding.js index f9fa43228d5..3a3d558361e 100644 --- a/lib/internal/encoding.js +++ b/lib/internal/encoding.js @@ -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'], diff --git a/test/parallel/test-whatwg-encoding-custom-textdecoder.js b/test/parallel/test-whatwg-encoding-custom-textdecoder.js index 74c6a002223..75a2a4735cd 100644 --- a/test/parallel/test-whatwg-encoding-custom-textdecoder.js +++ b/test/parallel/test-whatwg-encoding-custom-textdecoder.js @@ -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), ''); +} diff --git a/test/wpt/status/encoding.json b/test/wpt/status/encoding.json index 47d8c5f487f..ed98d966297 100644 --- a/test/wpt/status/encoding.json +++ b/test/wpt/status/encoding.json @@ -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": { |