Skip to content

Commit 6dce0f2

Browse files
authored
fix: Prevent stack overflow in portable fromCharCodes/CodePoints (#1340)
1 parent 0e398c4 commit 6dce0f2

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

Diff for: std/portable/index.js

+30-2
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,39 @@ globalScope["changetype"] = function changetype(value) {
198198
};
199199

200200
String["fromCharCodes"] = function fromCharCodes(arr) {
201-
return String.fromCharCode.apply(String, arr);
201+
const CHUNKSIZE = 1 << 13;
202+
const len = arr.length;
203+
if (len <= CHUNKSIZE) {
204+
return String.fromCharCode.apply(String, arr);
205+
}
206+
let index = 0;
207+
let parts = '';
208+
while (index < len) {
209+
parts += String.fromCharCode.apply(
210+
String,
211+
arr.slice(index, Math.min(index + CHUNKSIZE, len))
212+
);
213+
index += CHUNKSIZE;
214+
}
215+
return parts;
202216
};
203217

204218
String["fromCodePoints"] = function fromCodePoints(arr) {
205-
return String.fromCodePoint.apply(String, arr);
219+
const CHUNKSIZE = 1 << 13;
220+
const len = arr.length;
221+
if (len <= CHUNKSIZE) {
222+
return String.fromCodePoint.apply(String, arr);
223+
}
224+
let index = 0;
225+
let parts = '';
226+
while (index < len) {
227+
parts += String.fromCodePoint.apply(
228+
String,
229+
arr.slice(index, Math.min(index + CHUNKSIZE, len))
230+
);
231+
index += CHUNKSIZE;
232+
}
233+
return parts;
206234
};
207235

208236
if (!String.prototype.replaceAll) {

0 commit comments

Comments
 (0)