Co-authored-by: Gregory Noma <gregory.noma@gmail.com> Co-authored-by: Alyssa Clark <alyssa.clark@mongodb.com> Co-authored-by: Thomas Goyne <thomas.goyne@mongodb.com> Co-authored-by: Philip Stoev <philip.stoev@mongodb.com> Co-authored-by: Joan Bruguera Micó (at MongoDB) <joan.bruguera-mico@mongodb.com> Co-authored-by: Matthew Boros <mattBoros@users.noreply.github.com> Co-authored-by: Finley Lau <finley.lau@mongodb.com> Co-authored-by: Daniel Moody <dmoody256@gmail.com> Co-authored-by: Ted Tuckman <TedTuckman@users.noreply.github.com> Co-authored-by: Yuhong Zhang <yuhong.zhang@mongodb.com> Co-authored-by: Copilot <copilot@github.com> GitOrigin-RevId: 0a318ad31b0a7881549f93fb6bc0dd4cb57934e3
68 lines
1.7 KiB
JavaScript
68 lines
1.7 KiB
JavaScript
// SERVER-11781 Don't crash when converting deeply nested or cyclical JS objects to BSON.
|
|
// @tags: [
|
|
// # Uses $where operator
|
|
// requires_scripting,
|
|
// # TODO(SERVER-84158): Try to include this test(s).
|
|
// exclude_from_timeseries_crud_passthrough,
|
|
// # TODO SERVER-116054: Add support for $where.
|
|
// mozjs_wasm_unsupported,
|
|
// ]
|
|
|
|
function test() {
|
|
function assertTooBig(obj) {
|
|
// This used to crash rather than throwing an exception.
|
|
assert.throws(function () {
|
|
Object.bsonsize(obj);
|
|
});
|
|
}
|
|
|
|
function assertNotTooBig(obj) {
|
|
assert.doesNotThrow(function () {
|
|
Object.bsonsize(obj);
|
|
});
|
|
}
|
|
|
|
function objWithDepth(depth) {
|
|
let out = 1;
|
|
while (depth--) {
|
|
out = {o: out};
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function arrayWithDepth(depth) {
|
|
let out = 1;
|
|
while (depth--) {
|
|
out = [out];
|
|
}
|
|
return out;
|
|
}
|
|
|
|
assertNotTooBig({});
|
|
assertNotTooBig({array: []});
|
|
|
|
let objCycle = {};
|
|
objCycle.cycle = objCycle;
|
|
assertTooBig(objCycle);
|
|
|
|
let arrayCycle = [];
|
|
arrayCycle.push(arrayCycle);
|
|
assertTooBig({array: arrayCycle});
|
|
|
|
let objDepthLimit = 150;
|
|
assertNotTooBig(objWithDepth(objDepthLimit - 1));
|
|
assertTooBig(objWithDepth(objDepthLimit));
|
|
|
|
let arrayDepthLimit = objDepthLimit - 1; // one lower due to wrapping object
|
|
assertNotTooBig({array: arrayWithDepth(arrayDepthLimit - 1)});
|
|
assertTooBig({array: arrayWithDepth(arrayDepthLimit)});
|
|
}
|
|
|
|
// test in shell
|
|
test();
|
|
|
|
// test on server
|
|
db.depth_limit.drop();
|
|
db.depth_limit.insert({});
|
|
db.depth_limit.find({$where: test}).itcount(); // itcount ensures that cursor is executed on server
|