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

Unsafe global loop variables in js

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Minor - P4 Minor - P4
    • 3.1.8
    • None
    • Shell
    • None
    • Fully Compatible
    • ALL

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

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved: