[CONJS-115] Passing an empty array to batch throw an unexpected error Created: 2020-01-06  Updated: 2020-01-07  Resolved: 2020-01-07

Status: Closed
Project: MariaDB Connector/node.js
Component/s: API
Affects Version/s: None
Fix Version/s: 2.1.5

Type: Bug Priority: Major
Reporter: Diego Dupin Assignee: Diego Dupin
Resolution: Fixed Votes: 0
Labels: None


 Description   

see https://github.com/mariadb-corporation/mariadb-connector-nodejs/issues/94:
connection.batch('INSERT INTO ...', [ ])
will return a promise that rejects with the following

TypeError: Cannot read property 'length' of undefined
    at BulkPacket.writeHeader (.../node_modules/mariadb/lib/io/bulk-packet.js:100:29)
    at new BulkPacket (.../node_modules/mariadb/lib/io/bulk-packet.js:41:10)
    at BatchBulk.start (.../node_modules/mariadb/lib/cmd/batch-bulk.js:73:19)
    at _addCommandEnablePipeline (.../node_modules/mariadb/lib/connection.js:1046:11)
    at .../node_modules/mariadb/lib/connection.js:201:7
    at new Promise (<anonymous>)
    at Connection.batch (.../node_modules/mariadb/lib/connection.js:194:12)
    at test (.../app.js:40:20)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:189:7)

This arguably shouldn't reject since the idea of a batch is to apply the query to all elements in the array (which are themselves arrays) and an empty array should result in no queries being executed.

But the larger issue comes from calling pool.batch('INSERT INTO ...', [ ])

The promise rejects with the same error as above. But an uncaught error is also thrown:

 
TypeError: Cannot read property 'waitingResponseNo' of undefined
    at BatchBulk.success (.../node_modules/mariadb/lib/cmd/batch-bulk.js:142:10)
    at BatchBulk.readPrepareResultPacket (.../node_modules/mariadb/lib/cmd/resultset.js:165:21)
    at PacketInputStream.receivePacketBasic (.../node_modules/mariadb/lib/io/packet-input-stream.js:104:9)
    at PacketInputStream.onData (.../node_modules/mariadb/lib/io/packet-input-stream.js:160:20)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at Socket.Readable.push (_stream_readable.js:208:10)
    at TCP.onread (net.js:601:20)

This uncaught error crashes the node process (unless process.on('uncaughtException', ...) is used).

The expected behavior is for the library to never throw an uncaught error.

I would guess the bug relates to Usually an array of array, but in cases of only one placeholder per value, it can be given as a single array. i.e. the second argument is handled differently for pool.batch than for connection.batch


Generated at Thu Feb 08 03:22:52 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.