Skip to content

Commit

Permalink
Do not leak a global variable, and be safe also in _parseBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Jan 15, 2016
1 parent 38e00e1 commit c9bd395
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Parser.prototype.parse = function (buf) {
this._list.append(buf)

while ((this.packet.length != -1 || this._list.length > 0) &&
(noError = this[this._states[this._stateCounter]]())) {
this[this._states[this._stateCounter]]()) {
this._stateCounter++

if (this._stateCounter >= this._states.length) {
Expand Down Expand Up @@ -348,11 +348,12 @@ Parser.prototype._parseMessageId = function() {
Parser.prototype._parseString = function(maybeBuffer) {
var length = this._parseNum()
, result
, end = length + this._pos

if(length === -1 || length + this._pos > this._list.length || length + this._pos > this.packet.length)
if(length === -1 || end > this._list.length || end > this.packet.length)
return null

result = this._list.toString('utf8', this._pos, this._pos + length)
result = this._list.toString('utf8', this._pos, end)

this._pos += length

Expand All @@ -362,11 +363,12 @@ Parser.prototype._parseString = function(maybeBuffer) {
Parser.prototype._parseBuffer = function() {
var length = this._parseNum()
, result
, end = length + this._pos

if(length === -1 || length + this._pos > this._list.length)
if(length === -1 || end > this._list.length || end > this.packet.length)
return null

result = this._list.slice(this._pos, this._pos + length)
result = this._list.slice(this._pos, end)

this._pos += length

Expand Down

0 comments on commit c9bd395

Please sign in to comment.