Skip to content

Commit

Permalink
Fixed infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
ihusejnovic committed Oct 9, 2020
1 parent a530f60 commit 7c3b8c4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
15 changes: 11 additions & 4 deletions lib/pdu.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function PDU(command, options) {
}
}

PDU.fromStream = function(stream) {
PDU.commandLength = function(stream) {
var buffer = stream.read(4);
if (!buffer) {
return false;
Expand All @@ -50,12 +50,19 @@ PDU.fromStream = function(stream) {
throw Error('PDU length was too large (' + command_length +
', maximum is ' + PDU.maxLength + ').');
}
stream.unshift(buffer);
buffer = stream.read(command_length);
return command_length;
};

PDU.fromStream = function(stream, command_length) {
var buffer = stream.read(command_length - 4);
if (!buffer) {
return false;
}
return new PDU(buffer);
let buf = Buffer.alloc(4);
buf.writeUInt32BE(command_length);
var pduBuffer = Buffer.concat([buf, buffer]);

return new PDU(pduBuffer);
};

PDU.fromBuffer = function(buffer) {
Expand Down
10 changes: 9 additions & 1 deletion lib/smpp.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function Session(options) {
this._busy = false;
this._callbacks = [];
this._interval = 0;
this._command_length = null;
if (options.socket) {
this.socket = options.socket;
} else {
Expand Down Expand Up @@ -70,13 +71,20 @@ Session.prototype._extractPDUs = function() {
var pdu;
while (!this.paused) {
try {
if (!(pdu = PDU.fromStream(this.socket))) {
if(!this._command_length) {
this._command_length = PDU.commandLength(this.socket);
if(!this._command_length) {
break;
}
}
if (!(pdu = PDU.fromStream(this.socket, this._command_length))) {
break;
}
} catch (e) {
this.emit('error', e);
return;
}
this._command_length = null;
this.emit('pdu', pdu);
this.emit(pdu.command, pdu);
if (pdu.isResponse() && this._callbacks[pdu.sequence_number]) {
Expand Down
3 changes: 2 additions & 1 deletion test/pdu.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ describe('PDU', function() {
var readable = new stream.Readable();
readable._read = function() {} ;
readable.push(buffer);
var pdu = PDU.fromStream(readable);
var commandLength = PDU.commandLength(readable);
var pdu = PDU.fromStream(readable, commandLength);
assert.deepEqual(pdu, expected);
});
});
Expand Down

0 comments on commit 7c3b8c4

Please sign in to comment.