Description
Some loop variables are not declared as local, and so will use global scope vars if they exist. This leads to strangeness where the following is actually an infinite loop:
for (var i = 0; i < 100; i++) {
|
var rt = new ReplSetTest({ name: 'testSet', nodes: 1 });
|
rt.startSet();
|
rt.initiate();
|
// ...
|
rt.stopSet();
|
}
|
whereas it works if i is changed to j, or the whole thing is wrapped into a function.
The occurrences are:
$ ag '\bfor\((?! *var)' src/mongo/shell src/mongo/scripting
|
src/mongo/shell/servers.js
|
189: for( key in pathOpts ){
|
|
|
src/mongo/shell/shardingtest.js
|
610: for( i=0; i<this._alldbpaths.length; i++ ){
|
|
|
src/mongo/shell/collection.js
|
1478: for( shard in allSplitPoints ){
|
|
|
src/mongo/shell/replsettest.js
|
153: for(i=0; i<this.ports.length; i++) {
|
172: for(i=0; i<this.ports.length; i++) {
|
257: for( n = 0 ; n < this.ports.length; n++ ) {
|
804: for( i=0; i<this._alldbpaths.length; i++ ){
|
Luckily only jstests and esoteric functions are affected.
A full audit/lint of all js local variables can be left for SERVER-19167 (or some other more appropriate ticket).