diff options
author | Eliot Horowitz <eliot@10gen.com> | 2009-12-07 11:10:46 -0500 |
---|---|---|
committer | Eliot Horowitz <eliot@10gen.com> | 2009-12-07 11:10:46 -0500 |
commit | 57a6542753bed4a78d6bece5cb44143732b3e45e (patch) | |
tree | 63cd2a9f62f401bf38443e57f36b26c1be35f68d | |
parent | a705bdb4ddf5f2222c8c45df34438c42ecafb09e (diff) | |
download | mongo-57a6542753bed4a78d6bece5cb44143732b3e45e.tar.gz |
fix invalid ensureIndex crashing server SERVER-388
Conflicts:
db/pdfile.cpp
jstests/indexapi.js
-rw-r--r-- | db/pdfile.cpp | 2 | ||||
-rw-r--r-- | jstests/indexapi.js | 40 |
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" ); |