Permalink
Browse files

fix for infinite loop when message is fragmented

  • Loading branch information...
1 parent 7e87c12 commit 2ad1d528780d0925afa0a23d48d4a2e711f3b063 Di Wu committed Apr 19, 2013
Showing with 29 additions and 19 deletions.
  1. +9 −12 lib/backend_message.js
  2. +4 −5 lib/connection.js
  3. +9 −0 src/backend_message.coffee
  4. +7 −2 src/connection.coffee
@@ -126,12 +126,11 @@ BackendMessage.RowDescription = (function(_super) {
RowDescription.prototype.typeId = 84;
RowDescription.prototype.read = function(buffer) {
- var fieldDescriptor, formatCode, i, modifier, name, numberOfFields, pos, size, tableFieldIndex, tableOID, typeOID, _i, _results;
+ var fieldDescriptor, formatCode, i, modifier, name, numberOfFields, pos, size, tableFieldIndex, tableOID, typeOID, _i;
numberOfFields = buffer.readUInt16BE(0);
pos = 2;
this.columns = [];
- _results = [];
for (i = _i = 0; 0 <= numberOfFields ? _i < numberOfFields : _i > numberOfFields; i = 0 <= numberOfFields ? ++_i : --_i) {
name = buffer.readZeroTerminatedString(pos);
pos += Buffer.byteLength(name) + 1;
@@ -157,9 +156,9 @@ BackendMessage.RowDescription = (function(_super) {
modifier: modifier,
formatCode: formatCode
};
- _results.push(this.columns.push(fieldDescriptor));
+ this.columns.push(fieldDescriptor);
}
- return _results;
+ return void 0;
};
return RowDescription;
@@ -177,12 +176,11 @@ BackendMessage.DataRow = (function(_super) {
DataRow.prototype.typeId = 68;
DataRow.prototype.read = function(buffer) {
- var data, i, length, numberOfFields, pos, _i, _results;
+ var data, i, length, numberOfFields, pos, _i;
numberOfFields = buffer.readUInt16BE(0);
pos = 2;
this.values = [];
- _results = [];
for (i = _i = 0; 0 <= numberOfFields ? _i < numberOfFields : _i > numberOfFields; i = 0 <= numberOfFields ? ++_i : --_i) {
length = buffer.readUInt32BE(pos);
pos += 4;
@@ -192,9 +190,9 @@ BackendMessage.DataRow = (function(_super) {
data = buffer.slice(pos, pos + length);
pos += length;
}
- _results.push(this.values.push(data));
+ this.values.push(data);
}
- return _results;
+ return void 0;
};
return DataRow;
@@ -364,18 +362,17 @@ BackendMessage.CopyInResponse = (function(_super) {
CopyInResponse.prototype.typeId = 71;
CopyInResponse.prototype.read = function(buffer) {
- var i, numberOfFields, pos, _i, _results;
+ var i, numberOfFields, pos, _i;
this.globalFormatType = buffer.readUInt8(0);
this.fieldFormatTypes = [];
numberOfFields = buffer.readUInt16BE(1);
pos = 3;
- _results = [];
for (i = _i = 0; 0 <= numberOfFields ? _i < numberOfFields : _i > numberOfFields; i = 0 <= numberOfFields ? ++_i : --_i) {
this.fieldFormatTypes.push(buffer.readUInt8(pos));
- _results.push(pos += 1);
+ pos += 1;
}
- return _results;
+ return void 0;
};
return CopyInResponse;
View
@@ -318,7 +318,7 @@ Connection = (function(_super) {
};
Connection.prototype._onData = function(buffer) {
- var bufferedData, message, size, _results;
+ var bufferedData, message, size;
if (this.incomingData.length === 0) {
this.incomingData = buffer;
@@ -328,7 +328,6 @@ Connection = (function(_super) {
buffer.copy(bufferedData, this.incomingData.length);
this.incomingData = bufferedData;
}
- _results = [];
while (this.incomingData.length >= 5) {
size = this.incomingData.readUInt32BE(1);
if (size + 1 <= this.incomingData.length) {
@@ -338,12 +337,12 @@ Connection = (function(_super) {
}
this.emit('message', message);
this.emit(message.event, message);
- _results.push(this.incomingData = this.incomingData.slice(size + 1));
+ this.incomingData = this.incomingData.slice(size + 1);
} else {
- _results.push(void 0);
+ break;
}
}
- return _results;
+ return void 0;
};
Connection.prototype._onClose = function(error) {
@@ -88,6 +88,9 @@ class BackendMessage.RowDescription extends BackendMessage
@columns.push fieldDescriptor
+ # explicit return to avoid coffeescript generating result array
+ return undefined
+
class BackendMessage.DataRow extends BackendMessage
typeId: 68 # D
@@ -109,6 +112,9 @@ class BackendMessage.DataRow extends BackendMessage
@values.push(data)
+ # explicit return to avoid coffeescript generating result array
+ return undefined
+
class BackendMessage.CommandComplete extends BackendMessage
typeId: 67 # C
@@ -188,6 +194,9 @@ class BackendMessage.CopyInResponse extends BackendMessage
@fieldFormatTypes.push buffer.readUInt8(pos)
pos += 1
+ # explicit return to avoid coffeescript generating result array
+ return undefined
+
##############################################################
# BackendMessage factory
@@ -203,7 +203,7 @@ class Connection extends EventEmitter
# start at 1 to skip the message ID
while @incomingData.length >= 5
size = @incomingData.readUInt32BE(1)
-
+
if size + 1 <= @incomingData.length
# parse message
message = BackendMessage.fromBuffer(@incomingData.slice(0, size + 1))
@@ -213,8 +213,13 @@ class Connection extends EventEmitter
# update loop variables
@incomingData = @incomingData.slice(size + 1)
+ else
+ # message fragmented, keep collecting
+ break
+
+ # explicit return to avoid coffeescript generating result array
+ return undefined
-
_onClose: (error)->
@currentJob.onConnectionError("The connection was closed.") if @currentJob
@connected = false

0 comments on commit 2ad1d52

Please sign in to comment.