Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-278

RangeError during a find & sort query

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 2.0, 1.4.13
    • Affects Version/s: 1.4.12, 1.4.13
    • Component/s: None
    • Labels:
    • Environment:
      Node v0.10.32
      mongoDB 1.4.11
      Native driver 1.4.12
    • 2

      I got the following error using 1.4.12:

      [1:1]: RangeError: attempt to write beyond buffer bounds
      at Buffer.write (buffer.js:343:11)
      at packElement (/home/dev/GitHub/appserver-FF/node_modules/mongodb/node_modules/bson/lib/bson/bson.js:475:31)
      at serializeObject (/home/dev/GitHub/appserver-FF/node_modules/mongodb/node_modules/bson/lib/bson/bson.js:377:15)
      at Function.serializeWithBufferAndIndex (/home/dev/GitHub/appserver-FF/node_modules/mongodb/node_modules/bson/lib/bson/bson.js:343:10)
      at BSON.serializeWithBufferAndIndex (/home/dev/GitHub/appserver-FF/node_modules/mongodb/node_modules/bson/lib/bson/bson.js:1556:15)
      at QueryCommand.toBinary (/home/dev/GitHub/appserver-FF/node_modules/mongodb/lib/mongodb/commands/query_command.js:230:20)
      at Connection.write (/home/dev/GitHub/appserver-FF/node_modules/mongodb/lib/mongodb/connection/connection.js:264:37)
      at __executeQueryCommand (/home/dev/GitHub/appserver-FF/node_modules/mongodb/lib/mongodb/db.js:1739:16)
      at Db._executeQueryCommand (/home/dev/GitHub/appserver-FF/node_modules/mongodb/lib/mongodb/db.js:1889:7)
      at Cursor.nextObject (/home/dev/GitHub/appserver-FF/node_modules/mongodb/lib/mongodb/cursor.js:749:13)
      at Cursor.toArray (/home/dev/GitHub/appserver-FF/node_modules/mongodb/lib/mongodb/cursor.js:168:10)

      Here is a simple example to demonstrate the problem:

      require('mongodb').Db.connect('mongodb://127.0.0.1:27017/test', {}, function (err, db) {
          db.createCollection('collTest', function (err, col) {
              // The problem is a combination of the query being a string
              // instead of an object and the sort parameter being an
              // empty object instead of being undefined.
              col.find('val1').sort({}).toArray(function (err) {
                  if (err) { console.log('Here is the error:', err); }
                  db.close(true, function (err) {
                      console.log('Done.');
                  });
              });
          });
      });
      

      The problem seems to be a combination of the fact that I pass a string in the find() and an empty object in the sort().

      The following commands return without errors:

      col.find('val1').sort().toArray(...),
      col.find({prop1: 'val1'}).sort({}).toArray(...)
      col.find({prop1: 'val1'}).sort().toArray(...)
      

      This problem was introduced in 1.4.3. The code above does not generate an error in 1.4.2.

      My stack trace points to the lines added in revision 014e1b0869 .

      To solve my problem, I simply had to change the form of my find to use a proper object in the query parameter, but I wanted to tell you about this problem since the error message was very counter-intuitive and it took me some time to figure it out.

      Thanks.

            Assignee:
            christkv Christian Amor Kvalheim
            Reporter:
            nicolasp Nicolas Pelletier
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: