Skip to content

Commit

Permalink
SERVER-21388 validate captrunc argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Guo committed Jan 28, 2016
1 parent a2ad8f9 commit f2a6770
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
7 changes: 7 additions & 0 deletions jstests/noPassthroughWithMongod/capped_truncate.js
Expand Up @@ -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}));
}
Expand Down
6 changes: 6 additions & 0 deletions src/mongo/db/commands/test_commands.cpp
Expand Up @@ -189,7 +189,13 @@ class CapTrunc : public Command {
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);

Expand Down

0 comments on commit f2a6770

Please sign in to comment.