From f2a67700a3cfd9e401feb4a17931ea3209f10e4b Mon Sep 17 00:00:00 2001 From: Robert Guo Date: Wed, 11 Nov 2015 18:54:35 -0500 Subject: SERVER-21388 validate captrunc argument --- jstests/noPassthroughWithMongod/capped_truncate.js | 7 +++++++ src/mongo/db/commands/test_commands.cpp | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/jstests/noPassthroughWithMongod/capped_truncate.js b/jstests/noPassthroughWithMongod/capped_truncate.js index ddba2420bba..ce559919b80 100644 --- a/jstests/noPassthroughWithMongod/capped_truncate.js +++ b/jstests/noPassthroughWithMongod/capped_truncate.js @@ -11,6 +11,13 @@ autoIndexId: true })); var t = db.capped_truncate; + // It is an error to remove a non-positive number of documents. + assert.commandFailed(db.runCommand({ captrunc: "capped_truncate", n: -1 }), + "captrunc didn't return an error when attempting to remove a negative " + + "number of documents"); + assert.commandFailed(db.runCommand({ captrunc: "capped_truncate", n: 0 }), + "captrunc didn't return an error when attempting to remove 0 documents"); + for (var j = 1; j <= 10; j++) { assert.writeOK(t.insert({x:j})); } diff --git a/src/mongo/db/commands/test_commands.cpp b/src/mongo/db/commands/test_commands.cpp index 3cbc36519ba..9a2c5cec5cd 100644 --- a/src/mongo/db/commands/test_commands.cpp +++ b/src/mongo/db/commands/test_commands.cpp @@ -189,7 +189,13 @@ public: int n = cmdObj.getIntField("n"); bool inc = cmdObj.getBoolField("inc"); // inclusive range? + if (n <= 0) { + return appendCommandStatus(result, + {ErrorCodes::BadValue, "n must be a positive integer"}); + } + Client::WriteContext ctx(txn, nss.ns()); + Collection* collection = ctx.getCollection(); massert(13417, "captrunc collection not found or empty", collection); -- cgit v1.2.1