Skip to content

Commit 89f1af1

Browse files
committed
feat: handle weird input graceful
1 parent b028d39 commit 89f1af1

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ Internals
99

1010
- Due to the changes to ES6 the error performance improved by factor 2-3x
1111

12+
Features
13+
14+
- The parser now handles weird input graceful
15+
1216
## v.2.6.0 - 03 Apr, 2017
1317

1418
Internals

lib/parser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ function parseBulkString (parser) {
153153
if (length === undefined) {
154154
return
155155
}
156-
if (length === -1) {
156+
if (length < 0) {
157157
return null
158158
}
159159
const offsetEnd = parser.offset + length
@@ -203,7 +203,7 @@ function parseArray (parser) {
203203
if (length === undefined) {
204204
return
205205
}
206-
if (length === -1) {
206+
if (length < 0) {
207207
return null
208208
}
209209
const responses = new Array(length)

test/parsers.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,26 @@ describe('parsers', function () {
179179
assert.strictEqual(replyCount, 1)
180180
})
181181

182+
it('weird things', function () {
183+
var replyCount = 0
184+
var results = [[], '', [0, null, '', 0, '', []], 9223372036854776, '☃', [1, 'OK', null], null, 12345, [], null, 't']
185+
function checkReply (reply) {
186+
assert.deepEqual(results[replyCount], reply)
187+
replyCount++
188+
}
189+
var parser = newParser(checkReply)
190+
parser.execute(Buffer.from('*0\r\n$0\r\n\r\n*6\r\n:\r\n$-1\r\n$0\r\n\r\n:-\r\n$'))
191+
assert.strictEqual(replyCount, 2)
192+
parser.execute(Buffer.from('\r\n\r\n*\r\n:9223372036854775\r\n$' + Buffer.byteLength('☃') + '\r\n☃\r\n'))
193+
assert.strictEqual(replyCount, 5)
194+
parser.execute(Buffer.from('*3\r\n:1\r\n+OK\r\n$-1\r\n'))
195+
assert.strictEqual(replyCount, 6)
196+
parser.execute(Buffer.from('$-5'))
197+
assert.strictEqual(replyCount, 6)
198+
parser.execute(Buffer.from('\r\n:12345\r\n*-\r\n*-1\r\n+t\r\n'))
199+
assert.strictEqual(replyCount, 11)
200+
})
201+
182202
it('should not set the bufferOffset to a negative value', function (done) {
183203
if (Parser.name === 'HiredisReplyParser') {
184204
return this.skip()

0 commit comments

Comments
 (0)