handle errors better in parser.
This commit is contained in:
parent
17d9db5926
commit
dd8c92d5fd
@ -92,8 +92,11 @@ Parser.prototype.feed = function feed(data) {
|
|||||||
Parser.prototype.parse = function parse(chunk) {
|
Parser.prototype.parse = function parse(chunk) {
|
||||||
var checksum;
|
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));
|
return this._error('Packet too large: %dmb.', utils.mb(chunk.length));
|
||||||
|
}
|
||||||
|
|
||||||
if (this.packet === null) {
|
if (this.packet === null) {
|
||||||
this.packet = this.parseHeader(chunk) || {};
|
this.packet = this.parseHeader(chunk) || {};
|
||||||
@ -104,8 +107,11 @@ Parser.prototype.parse = function parse(chunk) {
|
|||||||
|
|
||||||
checksum = utils.checksum(this.packet.payload).readUInt32LE(0, true);
|
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');
|
return this._error('Invalid checksum');
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.packet.payload = this.parsePayload(this.packet.cmd, this.packet.payload);
|
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');
|
return this._error('Not NULL-terminated cmd');
|
||||||
|
|
||||||
cmd = h.toString('ascii', 4, 4 + i);
|
cmd = h.toString('ascii', 4, 4 + i);
|
||||||
|
|
||||||
this.waiting = h.readUInt32LE(16, true);
|
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 this._error('Packet length too large: %dmb', utils.mb(this.waiting));
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
cmd: cmd,
|
cmd: cmd,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user