summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEliot Horowitz <eliot@10gen.com>2009-12-07 11:10:46 -0500
committerEliot Horowitz <eliot@10gen.com>2009-12-07 11:10:46 -0500
commit57a6542753bed4a78d6bece5cb44143732b3e45e (patch)
tree63cd2a9f62f401bf38443e57f36b26c1be35f68d
parenta705bdb4ddf5f2222c8c45df34438c42ecafb09e (diff)
downloadmongo-57a6542753bed4a78d6bece5cb44143732b3e45e.tar.gz
fix invalid ensureIndex crashing server SERVER-388
Conflicts: db/pdfile.cpp jstests/indexapi.js
-rw-r--r--db/pdfile.cpp2
-rw-r--r--jstests/indexapi.js40
2 files changed, 42 insertions, 0 deletions
diff --git a/db/pdfile.cpp b/db/pdfile.cpp
index 9f054bbc05c..efcaf749b36 100644
--- a/db/pdfile.cpp
+++ b/db/pdfile.cpp
@@ -1156,6 +1156,7 @@ assert( !eloc.isNull() );
DiskLoc DataFileMgr::insert(const char *ns, const void *obuf, int len, bool god, const BSONElement &writeId, bool mayAddIndex) {
bool wouldAddIndex = false;
uassert("cannot insert into reserved $ collection", god || strchr(ns, '$') == 0 );
+ uassert("invalid ns", strchr( ns , '.' ) > 0 );
const char *sys = strstr(ns, "system.");
if ( sys ) {
uassert("attempt to insert in reserved database name 'system'", sys != ns);
@@ -1198,6 +1199,7 @@ assert( !eloc.isNull() );
const char *name = io.getStringField("name"); // name of the index
tabletoidxns = io.getStringField("ns"); // table it indexes
+ uassert( "invalid ns to index" , tabletoidxns.size() && tabletoidxns.find( '.' ) != string::npos );
if ( database->name != nsToClient(tabletoidxns.c_str()) ) {
uassert("bad table to index name on add index attempt", false);
return DiskLoc();
diff --git a/jstests/indexapi.js b/jstests/indexapi.js
new file mode 100644
index 00000000000..ae76ec74f57
--- /dev/null
+++ b/jstests/indexapi.js
@@ -0,0 +1,40 @@
+
+t = db.indexapi;
+t.drop();
+
+key = { x : 1 };
+
+c = { ns : t._fullName , key : key , name : t._genIndexName( key ) };
+assert.eq( c , t._indexSpec( { x : 1 } ) , "A" );
+
+c.name = "bob";
+assert.eq( c , t._indexSpec( { x : 1 } , "bob" ) , "B" );
+
+c.name = t._genIndexName( key );
+assert.eq( c , t._indexSpec( { x : 1 } ) , "C" );
+
+c.unique = true;
+assert.eq( c , t._indexSpec( { x : 1 } , true ) , "D" );
+assert.eq( c , t._indexSpec( { x : 1 } , [ true ] ) , "E" );
+assert.eq( c , t._indexSpec( { x : 1 } , { unique : true } ) , "F" );
+
+c.dropDups = true;
+assert.eq( c , t._indexSpec( { x : 1 } , [ true , true ] ) , "G" );
+assert.eq( c , t._indexSpec( { x : 1 } , { unique : true , dropDups : true } ) , "F" );
+
+t.ensureIndex( { x : 1 } , { unique : true } );
+idx = t.getIndexes();
+assert.eq( 2 , idx.length , "M1" );
+assert.eq( key , idx[1].key , "M2" );
+assert( idx[1].unique , "M3" );
+
+t.drop();
+t.ensureIndex( { x : 1 } , { unique : 1 } );
+idx = t.getIndexes();
+assert.eq( 2 , idx.length , "M1" );
+assert.eq( key , idx[1].key , "M2" );
+assert( idx[1].unique , "M3" );
+printjson( idx );
+
+db.system.indexes.insert( { ns : "test" , key : { x : 1 } , name : "x" } );
+assert( db.getLastError().indexOf( "invalid" ) >= 0 , "Z1" );