Skip to content

Commit 4d5332d

Browse files
committed
Parse default field of field packet correctly
1 parent 53a4d61 commit 4d5332d

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

Changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ you spot any mistakes.
1313
* Add `timeout` option to all sequences #855 #863
1414
* Default `connectTimeout` to 10 seconds
1515
* Fix domain binding with `conn.connect`
16+
* Fix `packet.default` to actually be a string
1617
* Fix `PROTOCOL_PACKETS_OUT_OF_ORDER` error to be catchable #844
1718
* Return `Query` object from `pool.query` like `conn.query` #830
1819
* Use `EventEmitter.listenerCount` when possible for faster counting

lib/protocol/Parser.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,11 @@ Parser.prototype.parseLengthCodedBuffer = function() {
166166
return this.parseBuffer(length);
167167
};
168168

169-
Parser.prototype.parseLengthCodedNumber = function() {
169+
Parser.prototype.parseLengthCodedNumber = function parseLengthCodedNumber() {
170+
if (this._offset >= this._buffer.length) {
171+
throw new Error('Parser: read past end');
172+
}
173+
170174
var bits = this._buffer[this._offset++];
171175

172176
if (bits <= 250) {

lib/protocol/packets/FieldPacket.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ function FieldPacket(options) {
88
this.orgTable = options.orgTable;
99
this.name = options.name;
1010
this.orgName = options.orgName;
11-
this.filler1 = undefined;
1211
this.charsetNr = options.charsetNr;
1312
this.length = options.length;
1413
this.type = options.type;
1514
this.flags = options.flags;
1615
this.decimals = options.decimals;
17-
this.filler2 = undefined;
1816
this.default = options.default;
1917
this.zeroFill = options.zeroFill;
2018
this.protocol41 = options.protocol41
@@ -28,13 +26,21 @@ FieldPacket.prototype.parse = function(parser) {
2826
this.orgTable = parser.parseLengthCodedString();
2927
this.name = parser.parseLengthCodedString();
3028
this.orgName = parser.parseLengthCodedString();
31-
this.filler1 = parser.parseFiller(1);
29+
30+
if (parser.parseLengthCodedNumber() !== 0x0c) {
31+
throw new TypeError('Received invalid field length');
32+
}
33+
3234
this.charsetNr = parser.parseUnsignedNumber(2);
3335
this.length = parser.parseUnsignedNumber(4);
3436
this.type = parser.parseUnsignedNumber(1);
3537
this.flags = parser.parseUnsignedNumber(2);
3638
this.decimals = parser.parseUnsignedNumber(1);
37-
this.filler2 = parser.parseFiller(2);
39+
40+
var filler = parser.parseBuffer(2);
41+
if (filler[0] !== 0x0 || filler[1] !== 0x0) {
42+
throw new TypeError('Received invalid filler');
43+
}
3844

3945
// parsed flags
4046
this.zeroFill = (this.flags & 0x0040 ? true : false);
@@ -43,7 +49,7 @@ FieldPacket.prototype.parse = function(parser) {
4349
return;
4450
}
4551

46-
this.default = parser.parseLengthCodedNumber();
52+
this.default = parser.parseLengthCodedString();
4753
} else {
4854
this.table = parser.parseLengthCodedString();
4955
this.name = parser.parseLengthCodedString();
@@ -60,7 +66,8 @@ FieldPacket.prototype.write = function(writer) {
6066
writer.writeLengthCodedString(this.orgTable);
6167
writer.writeLengthCodedString(this.name);
6268
writer.writeLengthCodedString(this.orgName);
63-
writer.writeFiller(1);
69+
70+
writer.writeLengthCodedNumber(0x0c);
6471
writer.writeUnsignedNumber(2, this.charsetNr || 0);
6572
writer.writeUnsignedNumber(4, this.length || 0);
6673
writer.writeUnsignedNumber(1, this.type || 0);

test/FakeServer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ FakeConnection.prototype._handleQueryPacket = function _handleQueryPacket(packet
130130
this._sendPacket(new Packets.FieldPacket({
131131
catalog : 'def',
132132
charsetNr : Charsets.UTF8_GENERAL_CI,
133+
default : '0',
133134
name : num,
134135
protocol41 : true,
135136
type : Types.LONG

0 commit comments

Comments
 (0)