diff options
author | Linus Groh <mail@linusgroh.de> | 2020-11-08 12:54:52 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-11-08 16:51:54 +0100 |
commit | a02b9983f9060d345af5efa439b268fe52a77692 (patch) | |
tree | eadb9a9a0ba23b133de23bd328a118cb9a9712be /Libraries/LibJS/Tests | |
parent | 9c3ead8f9107e73a0c5b453fb0dc49eacec1518c (diff) | |
download | serenity-a02b9983f9060d345af5efa439b268fe52a77692.zip |
LibJS: Throw RuntimeError when reaching the end of the stack
This prevents stack overflows when calling infinite/deep recursive
functions, e.g.:
const f = () => f(); f();
JSON.stringify({}, () => ({ foo: "bar" }));
new Proxy({}, { get: (_, __, p) => p.foo }).foo;
The VM caches a StackInfo object to not slow down function calls
considerably. VM::push_call_frame() will throw an exception if
necessary (plain Error with "RuntimeError" as its .name).
Diffstat (limited to 'Libraries/LibJS/Tests')
-rw-r--r-- | Libraries/LibJS/Tests/runtime-error-call-stack-size.js | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/Libraries/LibJS/Tests/runtime-error-call-stack-size.js b/Libraries/LibJS/Tests/runtime-error-call-stack-size.js new file mode 100644 index 0000000000..177bb2d675 --- /dev/null +++ b/Libraries/LibJS/Tests/runtime-error-call-stack-size.js @@ -0,0 +1,21 @@ +test("infinite recursion", () => { + function infiniteRecursion() { + infiniteRecursion(); + } + + try { + infiniteRecursion(); + } catch (e) { + expect(e).toBeInstanceOf(Error); + expect(e.name).toBe("RuntimeError"); + expect(e.message).toBe("Call stack size limit exceeded"); + } + + expect(() => { + JSON.stringify({}, () => ({ foo: "bar" })); + }).toThrowWithMessage(Error, "Call stack size limit exceeded"); + + expect(() => { + new Proxy({}, { get: (_, __, p) => p.foo }).foo; + }).toThrowWithMessage(Error, "Call stack size limit exceeded"); +}); |