mysql icon indicating copy to clipboard operation
mysql copied to clipboard

RangeError: out of range index

Open aniltallam opened this issue 5 years ago • 14 comments

following line in our code

mysql.createPool(connConfigs).getConnection(function (err, connection) {
...})

is sometimes causing following uncaught errors which we couldn't handle by simple try catck blocks

RangeError: out of range index, at RangeError (native), at Parser.parseBuffer (/srv/www/integrator/node_modules/mysql/lib/protocol/Parser.js:319:16), at Parser.parseFiller (/srv/www/integrator/node_modules/mysql/lib/protocol/Parser.js:276:15), at HandshakeInitializationPacket.parse (/srv/www/integrator/node_modules/mysql/lib/protocol/packets/HandshakeInitializationPacket.js:50:39), at Protocol._parsePacket (/srv/www/integrator/node_modules/mysql/lib/protocol/Protocol.js:262:12), at Parser.write (/srv/www/integrator/node_modules/mysql/lib/protocol/Parser.js:76:12), at Protocol.write (/srv/www/integrator/node_modules/mysql/lib/protocol/Protocol.js:39:16), at Socket. (/srv/www/integrator/node_modules/mysql/lib/Connection.js:103:28), at emitOne (events.js:96:13), at Socket.emit (events.js:188:7), at readableAddChunk (_stream_readable.js:176:18), at Socket.Readable.push (_stream_readable.js:134:10), at TCP.onread (net.js:547:20)

Looks proper error handling is missing when a handshake packet has incorrect data.

aniltallam avatar Jun 07 '19 12:06 aniltallam

Thanks for the report. What version of this module are you using? It doesn't seem to the be latest version based on the line numbers in the stack trace.

dougwilson avatar Jun 07 '19 12:06 dougwilson

we are using 2.14.0

aniltallam avatar Jun 07 '19 14:06 aniltallam

Can you try again with the latest version to see if the issue is resolved or not? Any fix would require you to upgrade anyway. I'm not able to test easily since you didn't provide any way to reproduce the issue.

dougwilson avatar Jun 07 '19 14:06 dougwilson

Thank you for the timely response. We saw these error messages in server logs. Even we couldn't reproduce the issue. But looking at mysqljs code, I think for an invalid handshake packet data, parsing (https://github.com/mysqljs/mysql/blob/v2.17.1/lib/protocol/packets/HandshakeInitializationPacket.js#L30) will throw error which has to be handled gracefully.

aniltallam avatar Jun 07 '19 14:06 aniltallam

Are you sure that error is not just coming back as the err argument in your callback? All parsing errors should be coming back in the callback, not through a try catch: https://github.com/mysqljs/mysql#error-handling

dougwilson avatar Jun 07 '19 14:06 dougwilson

I placed a throw new Error() on the line in your stack trace of the error, and indeed, it comes through the err argument in the callback. Unless you can provide a reproduction that shows otherwise or a pull request with what you think is a fix, there is nothing more I can do.

dougwilson avatar Jun 07 '19 15:06 dougwilson

After upgrading to 2.17.1, placed throw Error('Hello 123') at line no. 50 in HandshakeInitializationPacket.js but still, I am getting following uncaught error instead of getting the error in the callback of mysql.createPool(connConfigs).getConnection

Uncaught Error: Hello 123 at HandshakeInitializationPacket.parse (node_modules/mysql/lib/protocol/packets/HandshakeInitializationPacket.js:50:11) at Protocol._parsePacket (node_modules/mysql/lib/protocol/Protocol.js:272:12) at Parser._parsePacket (node_modules/mysql/lib/protocol/Parser.js:433:10) at Parser.write (node_modules/mysql/lib/protocol/Parser.js:43:10) at Protocol.write (node_modules/mysql/lib/protocol/Protocol.js:38:16) at Socket. (node_modules/mysql/lib/Connection.js:91:28) at Socket. (node_modules/mysql/lib/Connection.js:525:10) at Socket.EventEmitter.emit (domain.js:441:20) at addChunk (_stream_readable.js:284:12) at readableAddChunk (_stream_readable.js:265:11) at Socket.Readable.push (_stream_readable.js:220:10) at TCP.onStreamRead (internal/stream_base_commons.js:94:17) at TCP.onread (node_modules/async-listener/glue.js:188:31)

aniltallam avatar Jun 10 '19 09:06 aniltallam

Can you at least share the code you are running so I can run the same code when I place that thow?

dougwilson avatar Jun 10 '19 11:06 dougwilson

Created a PR #2235 to test the issue. in the pr, if you run test/unit/pool/test-connection-bad.js , following is the output

Anils-MacBook-Pro:mysql anilkumartallam$ node test/unit/pool/test-connection-bad.js
before pool.getConnection
/Users/anilkumartallam/workspace/external/mysql/lib/protocol/Parser.js:437
      throw err; // Rethrow non-MySQL errors
      ^

Error: Hello 123
    at HandshakeInitializationPacket.parse (/Users/anilkumartallam/workspace/external/mysql/lib/protocol/packets/HandshakeInitializationPacket.js:50:11)
    at Protocol._parsePacket (/Users/anilkumartallam/workspace/external/mysql/lib/protocol/Protocol.js:272:12)
    at Parser._parsePacket (/Users/anilkumartallam/workspace/external/mysql/lib/protocol/Parser.js:433:10)
    at Parser.write (/Users/anilkumartallam/workspace/external/mysql/lib/protocol/Parser.js:43:10)
    at Protocol.write (/Users/anilkumartallam/workspace/external/mysql/lib/protocol/Protocol.js:38:16)
    at Socket.<anonymous> (/Users/anilkumartallam/workspace/external/mysql/lib/Connection.js:91:28)
    at Socket.<anonymous> (/Users/anilkumartallam/workspace/external/mysql/lib/Connection.js:525:10)
    at Socket.emit (events.js:189:13)
    at addChunk (_stream_readable.js:284:12)
    at readableAddChunk (_stream_readable.js:265:11)

If you observe there is no "after pool.getConnection" text in above log.

aniltallam avatar Jun 12 '19 10:06 aniltallam

Any updates on this issue?

aniltallam avatar Jul 10 '19 06:07 aniltallam

Any updates?

aniltallam avatar Jul 23 '19 06:07 aniltallam

Any updates on this issue?

aniltallam avatar Sep 23 '19 10:09 aniltallam

Is there anyway to escalate the priority for this bug fix? Its been more than 3 months since bug reported.

aniltallam avatar Sep 23 '19 10:09 aniltallam

I will try to overhaul how packets are parsed soon. This is an open source project and providing a pull request with a fix is always going to be the fastest way to see a fix made available (as you're not going to be at the mercy of someone else's time to get around to writing the code). Alternatively a clear reproduction case (that is not modifying the code to just place a random throw statement) where it reproduces the issue with the module's code untouched to demonstrate the actual conditions to debug though quickly and try to make a fix that would address what is happening and allow for a test case to be created to validate the change.

dougwilson avatar Oct 04 '19 06:10 dougwilson