Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

SERVER-3702 printShardingStatus interprets database name as regular e…

…xpression

Merged pull request from: #142
Added a simple test for the pull request.
  • Loading branch information...
commit 46bc489a886d6d330a518ba8aaef05249b4798e3 1 parent 0e859c4
@renctan renctan authored
View
27 jstests/regex_util.js
@@ -0,0 +1,27 @@
+// Tests for RegExp.escape
+
+(function() {
+ var TEST_STRINGS = [
+ "[db]",
+ "{ab}",
+ "<c2>",
+ "(abc)",
+ "^first^",
+ "&addr",
+ "k@10gen.com",
+ "#4",
+ "!b",
+ "<>3",
+ "****word+",
+ "\t| |\n\r",
+ "Mongo-db",
+ "[{(<>)}]!@#%^&*+\\"
+ ];
+
+ TEST_STRINGS.forEach(function (str) {
+ var escaped = RegExp.escape(str);
+ var regex = new RegExp(escaped);
+ assert(regex.test(str), "Wrong escape for " + str);
+ });
+})();
+
View
6 src/mongo/shell/servers.js
@@ -125,7 +125,7 @@ MongoRunner.toRealPath = function( path, pathOpts ){
path = path.replace( /\$dataPath/g, MongoRunner.dataPath )
path = path.replace( /\$dataDir/g, MongoRunner.dataDir )
for( key in pathOpts ){
- path = path.replace( RegExp( "\\$" + key, "g" ), pathOpts[ key ] )
+ path = path.replace( RegExp( "\\$" + RegExp.escape(key), "g" ), pathOpts[ key ] )
}
// Relative path
@@ -1077,7 +1077,7 @@ printShardingStatus = function( configDB , verbose ){
output( "\t" + tojsononeline(db,"",true) );
if (db.partitioned){
- configDB.collections.find( { _id : new RegExp( "^" + db._id + "\\." ) } ).sort( { _id : 1 } ).forEach(
+ configDB.collections.find( { _id : new RegExp( "^" + RegExp.escape(db._id) + "\\." ) } ).sort( { _id : 1 } ).forEach(
function( coll ){
if ( coll.dropped == false ){
output("\t\t" + coll._id + " chunks:");
@@ -1143,7 +1143,7 @@ printShardingSizes = function(){
output( "\t" + tojson(db,"",true) );
if (db.partitioned){
- configDB.collections.find( { _id : new RegExp( "^" + db._id + "\." ) } ).sort( { _id : 1 } ).forEach(
+ configDB.collections.find( { _id : new RegExp( "^" + RegExp.escape(db._id) + "\." ) } ).sort( { _id : 1 } ).forEach(
function( coll ){
output("\t\t" + coll._id + " chunks:");
configDB.chunks.find( { "ns" : coll._id } ).sort( { min : 1 } ).forEach(
View
6 src/mongo/shell/utils.js
@@ -341,7 +341,7 @@ String.prototype.startsWith = function (str){
}
String.prototype.endsWith = function (str){
- return new RegExp( str + "$" ).test( this )
+ return new RegExp( RegExp.escape(str) + "$" ).test( this )
}
Number.prototype.zeroPad = function(width) {
@@ -427,6 +427,10 @@ ISODate = function(isoDateStr){
return new Date(time);
}
+RegExp.escape = function( text ){
+ return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
+}
+
RegExp.prototype.tojson = RegExp.prototype.toString;
Array.contains = function( a , x ){
View
2  src/mongo/shell/utils_sh.js
@@ -30,7 +30,7 @@ sh._dataFormat = function( bytes ){
}
sh._collRE = function( coll ){
- return RegExp( "^" + (coll + "").replace(/\./g, "\\.") + "-.*" )
+ return RegExp( "^" + RegExp.escape(coll + "") + "-.*" )
}
sh._pchunk = function( chunk ){
Please sign in to comment.
Something went wrong with that request. Please try again.