Skip to content

Commit e18600f

Browse files
committed
Fix edge cases when determing Query result packets
fixes #1547
1 parent 73d92f7 commit e18600f

File tree

2 files changed

+20
-26
lines changed

2 files changed

+20
-26
lines changed

Changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ you spot any mistakes.
99
* Accept regular expression as pool cluster pattern #1572
1010
* Accept wildcard anywhere in pool cluster pattern #1570
1111
* Add new error codes up to MySQL 5.7.17
12+
* Fix edge cases when determing Query result packets #1547
1213
* Fix memory leak when using long-running domains #1619 #1620
1314
* Update `bignumber.js` to 3.1.2
1415

lib/protocol/sequences/Query.js

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,38 +29,32 @@ Query.prototype.start = function() {
2929
this.emit('packet', new Packets.ComQueryPacket(this.sql));
3030
};
3131

32-
Query.prototype.determinePacket = function(firstByte, parser) {
33-
if (firstByte === 0) {
34-
// If we have a resultSet and got one eofPacket
35-
if (this._resultSet && this._resultSet.eofPackets.length === 1) {
36-
// Then this is a RowDataPacket with an empty string in the first column.
37-
// See: https://github.com/mysqljs/mysql/issues/222
38-
} else if (this._resultSet && this._resultSet.resultSetHeaderPacket
39-
&& this._resultSet.resultSetHeaderPacket.fieldCount !== null) {
40-
return Packets.FieldPacket;
41-
} else {
42-
return undefined;
32+
Query.prototype.determinePacket = function determinePacket(byte, parser) {
33+
var resultSet = this._resultSet;
34+
35+
if (!resultSet) {
36+
switch (byte) {
37+
case 0x00: return Packets.OkPacket;
38+
case 0xff: return Packets.ErrorPacket;
39+
default: return Packets.ResultSetHeaderPacket;
4340
}
4441
}
4542

46-
if (firstByte === 255) {
47-
return undefined;
43+
if (resultSet.eofPackets.length === 0) {
44+
return (resultSet.fieldPackets.length < resultSet.resultSetHeaderPacket.fieldCount)
45+
? Packets.FieldPacket
46+
: Packets.EofPacket;
4847
}
4948

50-
// EofPacket's are 5 bytes in mysql >= 4.1
51-
// This is the only / best way to differentiate their firstByte from a 9
52-
// byte length coded binary.
53-
if (firstByte === 0xfe && parser.packetLength() < 9) {
54-
return Packets.EofPacket;
49+
if (byte === 0xff) {
50+
return Packets.ErrorPacket;
5551
}
5652

57-
if (!this._resultSet) {
58-
return Packets.ResultSetHeaderPacket;
53+
if (byte === 0xfe && parser.packetLength() < 9) {
54+
return Packets.EofPacket;
5955
}
6056

61-
return (this._resultSet.eofPackets.length === 0)
62-
? Packets.FieldPacket
63-
: Packets.RowDataPacket;
57+
return Packets.RowDataPacket;
6458
};
6559

6660
Query.prototype['OkPacket'] = function(packet) {
@@ -95,11 +89,10 @@ Query.prototype['ErrorPacket'] = function(packet) {
9589
};
9690

9791
Query.prototype['ResultSetHeaderPacket'] = function(packet) {
98-
this._resultSet = new ResultSet(packet);
99-
100-
// used by LOAD DATA LOCAL INFILE queries
10192
if (packet.fieldCount === null) {
10293
this._sendLocalDataFile(packet.extra);
94+
} else {
95+
this._resultSet = new ResultSet(packet);
10396
}
10497
};
10598

0 commit comments

Comments
 (0)