Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON.stringify stack overflow tests #2517

Merged
merged 3 commits into from
Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ esid: sec-serializejsonproperty
description: >
Replacer function is called on properties, deleted during stringification.
info: |
JSON.stringify ( value [ , replacer [ , space ] ] )
SerializeJSONObject ( value )

[...]
12. Return ? SerializeJSONProperty(the empty String, wrapper).
5. If PropertyList is not undefined, then
[...]
6. Else,
a. Let K be ? EnumerableOwnPropertyNames(value, key).
[...]
8. For each element P of K, do
a. Let strP be ? SerializeJSONProperty(P, value).
[...]

SerializeJSONProperty ( key, holder )

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (C) 2020 Alexey Shvayka. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-json.stringify
description: >
Stack overflow due to infinite recursion in replacer throws an expected error.
---*/

var getStackOverflowError = function() {
try {
return getStackOverflowError();
} catch (err) {
return err;
}
};

var StackOverflowError = getStackOverflowError().constructor;

var obj = {};
var objReplacer = function() {
return {key: obj};
};

assert.throws(StackOverflowError, function() {
JSON.stringify(null, objReplacer);
});

var arr = [];
var arrReplacer = function() {
return [arr];
};

assert.throws(StackOverflowError, function() {
JSON.stringify(null, arrReplacer);
});
35 changes: 35 additions & 0 deletions test/built-ins/JSON/stringify/value-tojson-stack-overflow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (C) 2020 Alexey Shvayka. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-json.stringify
description: >
Stack overflow due to infinite recursion in toJSON throws an expected error.
---*/

var getStackOverflowError = function() {
try {
return getStackOverflowError();
} catch (err) {
return err;
}
};

var StackOverflowError = getStackOverflowError().constructor;

var obj = {};
obj.toJSON = function() {
return {key: obj};
};

assert.throws(StackOverflowError, function() {
JSON.stringify(obj);
});

var arr = [];
arr.toJSON = function() {
return [arr];
};

assert.throws(StackOverflowError, function() {
JSON.stringify(arr);
});