Bug 450633 - "script stack space quota is exhausted" exception in JSON.jsm when calling SessionStore API for sessions with a large amount of data. r=sayrer, sr=brendan

This commit is contained in:
Simon Bünzli 2008-09-02 08:36:15 +02:00
parent b7acee2c1f
commit 0b40c8a49f
2 changed files with 23 additions and 0 deletions

View File

@ -173,6 +173,22 @@ var JSON = {
const maybeHarmful = /[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/;
const jsonStrings = /"(\\.|[^"\\\n\r])*"/g;
const openEndedString = /"(\\.|[^"\\\n\r])*$/;
const maxStringLength = 1 << 16;
// process the string in several slices when it's too big in order
// to prevent script stack space quote exhaustion (cf. bug 450633)
while (aString.length > maxStringLength) {
let slice = aString.substr(0, maxStringLength).replace(jsonStrings, "");
aString = aString.substr(maxStringLength);
if (openEndedString.test(slice)) {
slice = slice.replace(openEndedString, "");
aString = '"' + aString;
}
if (maybeHarmful.test(slice))
return false;
}
return !maybeHarmful.test(aString.replace(jsonStrings, ""));
}
};

View File

@ -128,4 +128,11 @@ function run_test() {
do_check_false(JSON.isMostlyHarmless('(function() { alert("P0wn3d!"); })()'));
do_check_false(JSON.isMostlyHarmless('{ get a() { return "P0wn3d!"; } }'));
// this string shouldn't cause a "script stack space quota is exhausted" error
let bigString = " ";
while (bigString.length < (1 << 22))
bigString += bigString;
do_check_eq(JSON.fromString(toJSONString(bigString)), bigString);
}