Skip to content

Commit

Permalink
SERVER-22767 mongos segfault when invoking .explain() on certain oper…
Browse files Browse the repository at this point in the history
…ations
  • Loading branch information
WaleyChen committed Mar 7, 2016
1 parent a946a91 commit cfa10de
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
21 changes: 21 additions & 0 deletions jstests/noPassthrough/server22767.js
@@ -0,0 +1,21 @@
// test that the mongos doesn't segfault when it receives malformed BSON
var st = new ShardingTest({shards:1});
var testDB = st.getDB('test');
testDB.test.insert({a:1});

try {
testDB.test.find({key: {$regex: 'abcd\0xyz'}}).explain();
} catch (e) {
/*
* if the mongos segfaults, the error is the msg:
* "Error: error doing query: failed: network error while attempting to run command 'explain' on host '127.0.0.1:20014'"
*
* if the mongos doesn't segfault, the error is the object:
* "Error: explain failed: {
* "code" : 22,
* "ok" : 0,
* "errmsg" : "bson length doesn't match what we found in object with unknown _id"
* }"
*/
assert.eq(22, e.code);
}
10 changes: 8 additions & 2 deletions src/mongo/client/parallel.cpp
Expand Up @@ -849,8 +849,14 @@ void ParallelSortClusteredCursor::finishInit(OperationContext* txn) {
}
throw;
} else {
warning() << "db exception when finishing on " << shardId
<< ", current connection state is " << mdata.toBSON() << causedBy(e);
// the InvalidBSON exception indicates that the BSON is malformed ->
// don't print/call "mdata.toBSON()" to avoid unexpected errors e.g. a segfault
if (e.getCode() == 22)
warning() << "bson is malformed :: db exception when finishing on " << shardId
<< causedBy(e);
else
warning() << "db exception when finishing on " << shardId
<< ", current connection state is " << mdata.toBSON() << causedBy(e);
mdata.errored = true;
throw;
}
Expand Down

0 comments on commit cfa10de

Please sign in to comment.