Skip to content

Commit 5855dbc

Browse files
committed
Merge pull request #903 from NodeRedis/parser
Fix and simplify parser Fixes #875 Fixes #902
2 parents d00c5c1 + 5d08132 commit 5855dbc

File tree

3 files changed

+16
-34
lines changed

3 files changed

+16
-34
lines changed

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Changelog
22
=========
33

4+
Bugfixes
5+
6+
- Fixed a js parser error that could result in a timeout ([@BridgeAR](https://github.com/BridgeAR))
7+
48
## v.2.2.5 - 18 Oct, 2015
59

610
Bugfixes

lib/parsers/javascript.js

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,9 @@ JavascriptReplyParser.prototype._parseResult = function (type) {
2222

2323
if (type === 43 || type === 58 || type === 45) { // + or : or -
2424
// up to the delimiter
25-
end = this._packetEndOffset() - 1;
25+
end = this._packetEndOffset();
2626
start = this._offset;
2727

28-
if (end > this._buffer.length) {
29-
throw new IncompleteReadBuffer('Wait for more data.');
30-
}
31-
3228
// include the delimiter
3329
this._offset = end + 2;
3430

@@ -91,13 +87,15 @@ JavascriptReplyParser.prototype._parseResult = function (type) {
9187
}
9288

9389
return reply;
90+
} else {
91+
return null;
9492
}
9593
};
9694

9795
JavascriptReplyParser.prototype.execute = function (buffer) {
9896
this.append(buffer);
9997

100-
var type, ret, offset;
98+
var type, offset;
10199

102100
while (true) {
103101
offset = this._offset;
@@ -109,33 +107,17 @@ JavascriptReplyParser.prototype.execute = function (buffer) {
109107
try {
110108
type = this._buffer[this._offset++];
111109

112-
if (type === 43) { // Strings +
113-
ret = this._parseResult(type);
114-
115-
this.send_reply(ret);
110+
if (type === 43 || type === 58 || type === 36) { // Strings + // Integers : // Bulk strings $
111+
this.send_reply(this._parseResult(type));
116112
} else if (type === 45) { // Errors -
117-
ret = this._parseResult(type);
118-
119-
this.send_error(ret);
120-
} else if (type === 58) { // Integers :
121-
ret = this._parseResult(type);
122-
123-
this.send_reply(ret);
124-
} else if (type === 36) { // Bulk strings $
125-
ret = this._parseResult(type);
126-
127-
this.send_reply(ret);
113+
this.send_error(this._parseResult(type));
128114
} else if (type === 42) { // Arrays *
129115
// set a rewind point. if a failure occurs,
130116
// wait for the next execute()/append() and try again
131117
offset = this._offset - 1;
132118

133-
ret = this._parseResult(type);
134-
135-
this.send_reply(ret);
136-
} else if (type === 10 || type === 13) {
137-
break;
138-
} else {
119+
this.send_reply(this._parseResult(type));
120+
} else if (type !== 10 && type !== 13) {
139121
var err = new Error('Protocol error, got "' + String.fromCharCode(type) + '" as reply type byte');
140122
this.send_error(err);
141123
}
@@ -162,7 +144,7 @@ JavascriptReplyParser.prototype.append = function (newBuffer) {
162144
};
163145

164146
JavascriptReplyParser.prototype.parseHeader = function () {
165-
var end = this._packetEndOffset(),
147+
var end = this._packetEndOffset() + 1,
166148
value = this._buffer.toString('ascii', this._offset, end - 1) | 0;
167149

168150
this._offset = end + 1;
@@ -182,7 +164,6 @@ JavascriptReplyParser.prototype._packetEndOffset = function () {
182164
}
183165
}
184166

185-
offset++;
186167
return offset;
187168
};
188169

test/parser.spec.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('parsers', function () {
5555
return done();
5656
});
5757

58-
it('line breaks in the beginning', function (done) {
58+
it('line breaks in the beginning of the last chunk', function (done) {
5959
var parser = new Parser();
6060
var reply_count = 0;
6161
function check_reply(reply) {
@@ -68,10 +68,7 @@ describe('parsers', function () {
6868
parser.execute(new Buffer('*1\r\n*1\r\n$1\r\na'));
6969

7070
parser.execute(new Buffer('\r\n*1\r\n*1\r'));
71-
parser.execute(new Buffer('\n$1\r\na\r\n'));
72-
73-
parser.execute(new Buffer('*1\r\n*1\r\n'));
74-
parser.execute(new Buffer('$1\r\na\r\n'));
71+
parser.execute(new Buffer('\n$1\r\na\r\n*1\r\n*1\r\n$1\r\na\r\n'));
7572

7673
assert.equal(reply_count, 3, "check reply should have been called three times");
7774
return done();

0 commit comments

Comments
 (0)