فهرست منبع

Decode documents with TextDecoder instead of utf8To16()

utf8To16() built an array with one element per character, then a second
array of 3-character strings, then joined them into a string three times
the size, before handing it to decodeURIComponent().

Decoding a 100 MB document peaked at 3.4 GB of JS heap, measured in
isolation. Chrome caps the renderer heap at 4.4 GB and ignores
--max-old-space-size above it, so the tab died with no way around it.

TextDecoder does the same conversion in one pass. 'fatal: true' keeps the
previous behaviour of throwing on malformed UTF-8, which
decodeURIComponent did with a URIError.

utf8To16() had no other caller and is removed.

Fixes #2027
Gwénolé PAUGAM 1 ماه پیش
والد
کامیت
284cd67628
2فایلهای تغییر یافته به همراه2 افزوده شده و 25 حذف شده
  1. 1 0
      CHANGELOG.md
  2. 1 25
      js/privatebin.js

+ 1 - 0
CHANGELOG.md

@@ -8,6 +8,7 @@
 * CHANGED: Removed the unmaintained js-verify and replaced it with fast-check library.
 * CHANGED: Added a `jsconfig.json` in order to check the types of JavaScript.
 * CHANGED: Removed support for Bootstrap 3 as it requires jQuery.
+* FIXED: Decode decompressed documents with `TextDecoder` instead of `utf8To16()`, which allocated ~3.4 GB for a 100 MB document and killed the tab (#2027)
 * FIXED: Prevent browsers guessing MIME types on JSON(LD) API responses (#164)
 * FIXED: Insert quoted base path into JSON-LD documents
 

+ 1 - 25
js/privatebin.js

@@ -992,28 +992,6 @@ window.PrivateBin = (function () {
          */
         const base58 = new baseX('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz');
 
-        /**
-         * convert UTF-8 string stored in a DOMString to a standard UTF-16 DOMString
-         *
-         * Iterates over the bytes of the message, converting them all hexadecimal
-         * percent encoded representations, then URI decodes them all
-         *
-         * @name   CryptTool.utf8To16
-         * @function
-         * @private
-         * @param  {string} message UTF-8 string
-         * @return {string} UTF-16 string
-         */
-        function utf8To16(message) {
-            return decodeURIComponent(
-                message.split('').map(
-                    function (character) {
-                        return '%' + ('00' + character.charCodeAt(0).toString(16)).slice(-2);
-                    }
-                ).join('')
-            );
-        }
-
         /**
          * convert DOMString (UTF-16) to a UTF-8 string stored in a DOMString
          *
@@ -1120,9 +1098,7 @@ window.PrivateBin = (function () {
                     new Uint8Array(data)
                 ).buffer;
             }
-            return utf8To16(
-                arraybufferToString(data)
-            );
+            return new TextDecoder('utf-8', {fatal: true}).decode(data);
         }
 
         /**