Skip to content

Commit 56861a8

Browse files
author
Ruben Bridgewater
committed
Escape js parser protocol error characters
1 parent f37d5ed commit 56861a8

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

lib/parsers/javascript.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ JavascriptReplyParser.prototype.run = function (buffer) {
137137
if (this._type !== undefined && this._protocol_error === true) {
138138
// Reset the buffer so the parser can handle following commands properly
139139
this._buffer = new Buffer(0);
140-
this.send_error(new Error('Protocol error, got "' + String.fromCharCode(this._type) + '" as reply type byte'));
140+
this.send_error(new Error('Protocol error, got ' + JSON.stringify(String.fromCharCode(this._type)) + ' as reply type byte'));
141141
}
142142
};
143143

test/parser.spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,45 @@ describe('parsers', function () {
7373
assert.strictEqual(err_count, 1);
7474
});
7575

76+
it('parser error v3', function () {
77+
var parser = new Parser();
78+
var reply_count = 0;
79+
var err_count = 0;
80+
function check_reply (reply) {
81+
reply = utils.reply_to_strings(reply);
82+
assert.strictEqual(reply[0], 'OK');
83+
reply_count++;
84+
}
85+
function check_error (err) {
86+
assert.strictEqual(err.message, 'Protocol error, got "\\n" as reply type byte');
87+
err_count++;
88+
}
89+
parser.send_error = check_error;
90+
parser.send_reply = check_reply;
91+
92+
parser.execute(new Buffer('*1\r\n+OK\r\n\n+zasd\r\n'));
93+
assert.strictEqual(reply_count, 1);
94+
assert.strictEqual(err_count, 1);
95+
});
96+
97+
it('should handle \\r and \\n characters properly', function () {
98+
// If a string contains \r or \n characters it will always be send as a bulk string
99+
var parser = new Parser();
100+
var reply_count = 0;
101+
var entries = ['foo\r', 'foo\r\nbar', '\r\nfoo', 'foo\r\n'];
102+
function check_reply (reply) {
103+
reply = utils.reply_to_strings(reply);
104+
assert.strictEqual(reply, entries[reply_count]);
105+
reply_count++;
106+
}
107+
parser.send_reply = check_reply;
108+
109+
parser.execute(new Buffer('$4\r\nfoo\r\r\n$8\r\nfoo\r\nbar\r\n$5\r\n\r\n'));
110+
assert.strictEqual(reply_count, 2);
111+
parser.execute(new Buffer('foo\r\n$5\r\nfoo\r\n\r\n'));
112+
assert.strictEqual(reply_count, 4);
113+
});
114+
76115
it('line breaks in the beginning of the last chunk', function () {
77116
var parser = new Parser();
78117
var reply_count = 0;

0 commit comments

Comments
 (0)