Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-19841

Unsafe global loop variables in js

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 3.1.8
    • Affects Version/s: None
    • Component/s: Shell
    • None
    • Fully Compatible
    • ALL
    • None
    • 3
    • None
    • None
    • None
    • None
    • None
    • None

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

            Assignee:
            kevin.pulo@mongodb.com Kevin Pulo
            Reporter:
            kevin.pulo@mongodb.com Kevin Pulo
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: