Permalink
Browse files

better error handling for invalid objcet ids SERVER-88

  • Loading branch information...
1 parent 01397d2 commit f53f8553472bf350f5ca3986f9f50fe1e2d03e33 @erh erh committed Jun 2, 2009
Showing with 20 additions and 1 deletion.
  1. +2 −0 jstests/objid4.js
  2. +18 −1 scripting/sm_db.cpp
View
@@ -12,3 +12,5 @@ b = ObjectId( o.str );
assert.eq( o.str , b.str );
assert.eq( b.str , b.str.toString() )
+assert.throws( function(z){ return new ObjectId( "a" ); } );
+assert.throws( function(z){ return new ObjectId( "12345678901234567890123z" ); } );
View
@@ -398,7 +398,24 @@ namespace mongo {
}
else {
uassert( "object_id_constructor can't take more than 1 param" , argc == 1 );
- oid.init( c.toString( argv[0] ) );
+ string s = c.toString( argv[0] );
+
+ if ( s.size() != 24 ){
+ JS_ReportError( cx , "invalid object id: length" );
+ return JS_FALSE;
+ }
+
+ for ( string::size_type i=0; i<s.size(); i++ ){
+ char c = s[i];
+ if ( ( c >= '0' && c <= '9' ) ||
+ ( c >= 'a' && c <= 'f' ) ||
+ ( c >= 'A' && c <= 'F' ) ){
+ continue;
+ }
+ JS_ReportError( cx , "invalid object id: not hex" );
+ return JS_FALSE;
+ }
+ oid.init( s );
}
if ( ! JS_InstanceOf( cx , obj , &object_id_class , 0 ) ){

0 comments on commit f53f855

Please sign in to comment.