handle errors better in parser.

This commit is contained in:
Christopher Jeffrey 2016-05-18 18:54:46 -07:00
parent 17d9db5926
commit dd8c92d5fd
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -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,