Skip to content

Commit 0d1541d

Browse files
rhodeycharmanderbrianc
authored
Always check if activeQuery is null before using it (#3586)
* check that activeQuery is not null every time before use and emit an error if is. * docs: code style consistency * Add more tests --------- Co-authored-by: Charmander <~@charmander.me> Co-authored-by: Brian Carlson <brian.m.carlson@gmail.com>
1 parent d24ca25 commit 0d1541d

3 files changed

Lines changed: 64 additions & 6 deletions

File tree

packages/pg/lib/client.js

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -407,23 +407,47 @@ class Client extends EventEmitter {
407407
}
408408

409409
_handleRowDescription(msg) {
410+
const activeQuery = this._getActiveQuery()
411+
if (activeQuery == null) {
412+
const error = new Error('Received unexpected rowDescription message from backend.')
413+
this._handleErrorEvent(error)
414+
return
415+
}
410416
// delegate rowDescription to active query
411-
this._getActiveQuery().handleRowDescription(msg)
417+
activeQuery.handleRowDescription(msg)
412418
}
413419

414420
_handleDataRow(msg) {
421+
const activeQuery = this._getActiveQuery()
422+
if (activeQuery == null) {
423+
const error = new Error('Received unexpected dataRow message from backend.')
424+
this._handleErrorEvent(error)
425+
return
426+
}
415427
// delegate dataRow to active query
416-
this._getActiveQuery().handleDataRow(msg)
428+
activeQuery.handleDataRow(msg)
417429
}
418430

419431
_handlePortalSuspended(msg) {
432+
const activeQuery = this._getActiveQuery()
433+
if (activeQuery == null) {
434+
const error = new Error('Received unexpected portalSuspended message from backend.')
435+
this._handleErrorEvent(error)
436+
return
437+
}
420438
// delegate portalSuspended to active query
421-
this._getActiveQuery().handlePortalSuspended(this.connection)
439+
activeQuery.handlePortalSuspended(this.connection)
422440
}
423441

424442
_handleEmptyQuery(msg) {
443+
const activeQuery = this._getActiveQuery()
444+
if (activeQuery == null) {
445+
const error = new Error('Received unexpected emptyQuery message from backend.')
446+
this._handleErrorEvent(error)
447+
return
448+
}
425449
// delegate emptyQuery to active query
426-
this._getActiveQuery().handleEmptyQuery(this.connection)
450+
activeQuery.handleEmptyQuery(this.connection)
427451
}
428452

429453
_handleCommandComplete(msg) {
@@ -453,11 +477,23 @@ class Client extends EventEmitter {
453477
}
454478

455479
_handleCopyInResponse(msg) {
456-
this._getActiveQuery().handleCopyInResponse(this.connection)
480+
const activeQuery = this._getActiveQuery()
481+
if (activeQuery == null) {
482+
const error = new Error('Received unexpected copyInResponse message from backend.')
483+
this._handleErrorEvent(error)
484+
return
485+
}
486+
activeQuery.handleCopyInResponse(this.connection)
457487
}
458488

459489
_handleCopyData(msg) {
460-
this._getActiveQuery().handleCopyData(msg, this.connection)
490+
const activeQuery = this._getActiveQuery()
491+
if (activeQuery == null) {
492+
const error = new Error('Received unexpected copyData message from backend.')
493+
this._handleErrorEvent(error)
494+
return
495+
}
496+
activeQuery.handleCopyData(msg, this.connection)
461497
}
462498

463499
_handleNotification(msg) {

packages/pg/test/integration/gh-issues/3174-tests.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,10 @@ const testErrorBuffer = (bufferName, errorBuffer) => {
165165
if (!helper.args.native) {
166166
testErrorBuffer('parseComplete', buffers.parseComplete())
167167
testErrorBuffer('commandComplete', buffers.commandComplete('f'))
168+
testErrorBuffer('rowDescription', buffers.rowDescription())
169+
testErrorBuffer('dataRow', buffers.dataRow())
170+
testErrorBuffer('portalSuspended', buffers.portalSuspended())
171+
testErrorBuffer('emptyQuery', buffers.emptyQuery())
172+
testErrorBuffer('copyIn', buffers.copyIn(0))
173+
testErrorBuffer('copyData', buffers.copyData(Buffer.from([1, 2, 3])))
168174
}

packages/pg/test/test-buffers.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,20 @@ buffers.portalSuspended = function () {
118118
return new BufferList().join(true, 's')
119119
}
120120

121+
buffers.copyIn = function (cols) {
122+
const list = new BufferList()
123+
// text mode
124+
.add(Buffer.from([0]))
125+
// column count
126+
.addInt16(cols)
127+
for (let i = 0; i < cols; i++) {
128+
list.addInt16(i)
129+
}
130+
return list.join(true, 'G')
131+
}
132+
133+
buffers.copyData = function (bytes) {
134+
return new BufferList().add(bytes).join(true, 'd')
135+
}
136+
121137
module.exports = buffers

0 commit comments

Comments
 (0)