[SERVER-19841] Unsafe global loop variables in js Created: 10/Aug/15  Updated: 19/Sep/15  Resolved: 10/Sep/15

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: None
Fix Version/s: 3.1.8

Type: Bug Priority: Minor - P4
Reporter: Kevin Pulo Assignee: Kevin Pulo
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Operating System: ALL
Participants:

 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).



 Comments   
Comment by Githook User [ 10/Sep/15 ]

Author:

{u'username': u'devkev', u'name': u'Kevin Pulo', u'email': u'kevin.pulo@mongodb.com'}

Message: SERVER-19841: Fix unsafe global loop vars in js
Branch: master
https://github.com/mongodb/mongo/commit/405e3201e794d011e35972a9142f29fa6fde2754

Generated at Thu Feb 08 03:52:14 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.