Skip to content

Commit

Permalink
fix: Prevent stack overflow in portable fromCharCodes/CodePoints (#1340)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxGraey authored Jun 14, 2020
1 parent 0e398c4 commit 6dce0f2
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions std/portable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,39 @@ globalScope["changetype"] = function changetype(value) {
};

String["fromCharCodes"] = function fromCharCodes(arr) {
return String.fromCharCode.apply(String, arr);
const CHUNKSIZE = 1 << 13;
const len = arr.length;
if (len <= CHUNKSIZE) {
return String.fromCharCode.apply(String, arr);
}
let index = 0;
let parts = '';
while (index < len) {
parts += String.fromCharCode.apply(
String,
arr.slice(index, Math.min(index + CHUNKSIZE, len))
);
index += CHUNKSIZE;
}
return parts;
};

String["fromCodePoints"] = function fromCodePoints(arr) {
return String.fromCodePoint.apply(String, arr);
const CHUNKSIZE = 1 << 13;
const len = arr.length;
if (len <= CHUNKSIZE) {
return String.fromCodePoint.apply(String, arr);
}
let index = 0;
let parts = '';
while (index < len) {
parts += String.fromCodePoint.apply(
String,
arr.slice(index, Math.min(index + CHUNKSIZE, len))
);
index += CHUNKSIZE;
}
return parts;
};

if (!String.prototype.replaceAll) {
Expand Down

0 comments on commit 6dce0f2

Please sign in to comment.