From 74a93fdf63a17de990954399b10eb6dfe7dd1973 Mon Sep 17 00:00:00 2001 From: Andreu Botella Date: Thu, 28 Oct 2021 14:17:27 -0700 Subject: fix(webidl): Don't throw when converting a detached buffer source (#12585) The Web IDL conversion to `BufferSource` and similar types shouldn't check whether the buffer is detached. In the case of `TextDecoder`, our implementation would still throw after the Web IDL conversions because we're creating a new `Uint8Array` from the buffer source's buffer, which throws if it's detached. This change also fixes this bug. --- ext/web/08_text_encoding.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'ext/web/08_text_encoding.js') diff --git a/ext/web/08_text_encoding.js b/ext/web/08_text_encoding.js index d3f6d45fb..d2922a6d4 100644 --- a/ext/web/08_text_encoding.js +++ b/ext/web/08_text_encoding.js @@ -105,14 +105,19 @@ } try { - if (ArrayBufferIsView(input)) { - input = new Uint8Array( - input.buffer, - input.byteOffset, - input.byteLength, - ); - } else { - input = new Uint8Array(input); + try { + if (ArrayBufferIsView(input)) { + input = new Uint8Array( + input.buffer, + input.byteOffset, + input.byteLength, + ); + } else { + input = new Uint8Array(input); + } + } catch { + // If the buffer is detached, just create a new empty Uint8Array. + input = new Uint8Array(); } if (input.buffer instanceof SharedArrayBuffer) { // We clone the data into a non-shared ArrayBuffer so we can pass it -- cgit v1.2.3