Skip to content

Commit

Permalink
Merge pull request #86 from colinmutter/fix/range-error
Browse files Browse the repository at this point in the history
Fix for range error when attempting to read from buffer
  • Loading branch information
nlf committed Oct 28, 2014
2 parents 72c6e44 + d3a577f commit e1892b7
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,23 +121,44 @@ ConnectionManager.prototype._receive = function (data) {

var length;

if (this.readBufPos) {
// Manually disable coverage here. This is known to cause an issue
// in some cases, but has been unreproducable with a test.
/* $lab:coverage:off$ */
if (this.readBuf.length < this.readBufPos + data.length) {
this._growReadBuffer(this.readBufPos + data.length);
}
/* $lab:coverage:on$ */
// Make sure we can accomodate the incoming data
// Manually disable coverage here. This is known to cause an issue
// in some cases, but has been unreproducable with a test.
/* $lab:coverage:off$ */
if (this.readBuf.length < this.readBufPos + data.length) {
this._growReadBuffer(this.readBufPos + data.length);
}
/* $lab:coverage:on$ */

// If we have data in the read buffer already, append and
// replace this `data` with the entirety of the buffer
if (this.readBufPos) {
data.copy(this.readBuf, this.readBufPos);
this.readBufPos += data.length;
data = this.readBuf.slice(0, this.readBufPos);
}

// If we don't have enough data to even constitute the `length` bit from the
// protocol, we're going to have to wait for more data
/* $lab:coverage:off$ */
if (data.length < 4) {
// Accomodate in read buffer
this._growReadBuffer(this.readBufPos + data.length);
data.copy(this.readBuf, this.readBufPos);
this.readBufPos += data.length;
// Wait for the rest of the message...
return;
}
/* $lab:coverage:on$ */

// Read the protocol `message length`
length = data.readInt32BE(0);

// If the current read buffer has less data
// than the protocol message *should* have, copy and wait for more
if (data.length < 4 + length) {
// If the buffer read position is > 0,
// then we have already appended it above.
if (this.readBufPos === 0) {
this._growReadBuffer(4 + length);
data.copy(this.readBuf);
Expand Down Expand Up @@ -249,4 +270,4 @@ ConnectionManager.prototype.makeRequest = function (options, callback) {
this._processNext(callback);
};

module.exports = ConnectionManager;
module.exports = ConnectionManager;

0 comments on commit e1892b7

Please sign in to comment.