From dd8c92d5fdf60805815633bdc3827ac2648ad672 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 18 May 2016 18:54:46 -0700 Subject: [PATCH] handle errors better in parser. --- lib/bcoin/protocol/parser.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/bcoin/protocol/parser.js b/lib/bcoin/protocol/parser.js index 273f7374..eb24aa1f 100644 --- a/lib/bcoin/protocol/parser.js +++ b/lib/bcoin/protocol/parser.js @@ -92,8 +92,11 @@ Parser.prototype.feed = function feed(data) { Parser.prototype.parse = function parse(chunk) { var checksum; - if (chunk.length > constants.MAX_MESSAGE) + if (chunk.length > constants.MAX_MESSAGE) { + this.waiting = 24; + this.packet = null; return this._error('Packet too large: %dmb.', utils.mb(chunk.length)); + } if (this.packet === null) { this.packet = this.parseHeader(chunk) || {}; @@ -104,8 +107,11 @@ Parser.prototype.parse = function parse(chunk) { checksum = utils.checksum(this.packet.payload).readUInt32LE(0, true); - if (checksum !== this.packet.checksum) + if (checksum !== this.packet.checksum) { + this.waiting = 24; + this.packet = null; return this._error('Invalid checksum'); + } try { this.packet.payload = this.parsePayload(this.packet.cmd, this.packet.payload); @@ -143,10 +149,13 @@ Parser.prototype.parseHeader = function parseHeader(h) { return this._error('Not NULL-terminated cmd'); cmd = h.toString('ascii', 4, 4 + i); + this.waiting = h.readUInt32LE(16, true); - if (this.waiting > constants.MAX_MESSAGE) + if (this.waiting > constants.MAX_MESSAGE) { + this.waiting = 24; return this._error('Packet length too large: %dmb', utils.mb(this.waiting)); + } return { cmd: cmd,