summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough
diff options
context:
space:
mode:
authorMohammad Dashti <mdashti@gmail.com>2022-04-28 18:05:50 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-04-28 19:15:54 +0000
commit6808bcf11efcb0b2e1af095093803a61326cb4c6 (patch)
treee72835220ecc35e77d05b4d0b360fd082499aaa3 /jstests/noPassthrough
parent3559fdadbc1abb6fa20ad4f7910d32b7e5c407da (diff)
downloadmongo-6808bcf11efcb0b2e1af095093803a61326cb4c6.tar.gz
SERVER-65773 Improved MozJS error handling
Diffstat (limited to 'jstests/noPassthrough')
-rw-r--r--jstests/noPassthrough/shell_infinite_recursion.js38
1 files changed, 38 insertions, 0 deletions
diff --git a/jstests/noPassthrough/shell_infinite_recursion.js b/jstests/noPassthrough/shell_infinite_recursion.js
new file mode 100644
index 00000000000..e89d888e321
--- /dev/null
+++ b/jstests/noPassthrough/shell_infinite_recursion.js
@@ -0,0 +1,38 @@
+// This test checks that an infinite recursion correctly produces an 'InternalError: too much
+// recursion' error and does not crash the shell.
+(function() {
+"use strict";
+
+const makeBinData = () => BinData(4, "gf1UcxdHTJ2HQ/EGQrO7mQ==");
+const makeUUID = () => UUID("81fd5473-1747-4c9d-8743-f10642b3bb99");
+const makeHexData = () => new HexData(4, "81fd547317474c9d8743f10642b3bb99");
+
+function infiniteRecursionGen(fn) {
+ return function() {
+ let testRecursiveFn = () => {
+ let y = fn();
+ return testRecursiveFn(y);
+ };
+ let x = testRecursiveFn(1);
+ return x;
+ };
+}
+
+function assertThrowsInfiniteRecursion(fn) {
+ const err = assert.throws(fn, [], "Infinite recursion should throw an error.");
+ assert(/too much recursion/.test(err.message),
+ `Error wasn't caused by infinite recursion: ${err.toString()}\n${err.stack}`);
+
+ // The choice of 20 for the number of frames is somewhat arbitrary. We check for there to be
+ // some reasonable number of stack frames because most regressions would cause the stack to
+ // contain a single frame or none at all.
+ const kMinExpectedStack = 20;
+ assert.gte(err.stack.split("\n").length,
+ kMinExpectedStack,
+ `Error didn't preserve the JavaScript stacktrace: ${err.toString()}\n${err.stack}`);
+}
+
+assertThrowsInfiniteRecursion(infiniteRecursionGen(makeBinData));
+assertThrowsInfiniteRecursion(infiniteRecursionGen(makeUUID));
+assertThrowsInfiniteRecursion(infiniteRecursionGen(makeHexData));
+})();