Skip to content

Commit

Permalink
Fix multi byte characters getting corrupted
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben Bridgewater committed Jul 21, 2016
1 parent 3ca5848 commit 7b4dbf2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v.2.0.4 - 21 Jul, 2016

Bugfixes

- Fixed multi byte characters getting corrupted

## v.2.0.3 - 17 Jun, 2016

Bugfixes
Expand Down
8 changes: 5 additions & 3 deletions lib/parser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

var StringDecoder = require('string_decoder').StringDecoder
var decoder = new StringDecoder()
var ReplyError = require('./replyError')
var bufferPool = new Buffer(32 * 1024)
var bufferOffset = 0
Expand Down Expand Up @@ -295,12 +297,12 @@ function concatBulkString (parser) {
} else {
chunks++
}
var res = list[0].toString('utf8', parser.bigOffset)
var res = decoder.write(list[0].slice(parser.bigOffset))
for (var i = 1; i < chunks - 2; i++) {
// We are only safe to fully add up elements that are neither the first nor any of the last two elements
res += list[i].toString()
res += decoder.write(list[i])
}
res += list[i].toString('utf8', 0, offset === 1 ? list[i].length - 1 : offset - 2)
res += decoder.end(list[i].slice(0, offset === 1 ? list[i].length - 1 : offset - 2))
return res
}

Expand Down
8 changes: 5 additions & 3 deletions test/parsers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ describe('parsers', function () {
it('should handle \\r and \\n characters properly', function () {
// If a string contains \r or \n characters it will always be send as a bulk string
var replyCount = 0
var entries = ['foo\r', 'foo\r\nbar', '\r\nfoo', 'foo\r\n', 'foo', 'foobar', 'foo\r', 'äfooöü', 'abc']
var entries = ['foo\r', 'foo\r\nbar', '\r\nСанкт-Пет', 'foo\r\n', 'foo', 'foobar', 'foo\r', 'äfooöü', 'abc']
function checkReply (reply) {
assert.strictEqual(reply, entries[replyCount])
replyCount++
Expand All @@ -300,9 +300,11 @@ describe('parsers', function () {
returnFatalError: returnFatalError
})

parser.execute(new Buffer('$4\r\nfoo\r\r\n$8\r\nfoo\r\nbar\r\n$5\r\n\r\n'))
parser.execute(new Buffer('$4\r\nfoo\r\r\n$8\r\nfoo\r\nbar\r\n$19\r\n\r\n'))
parser.execute(new Buffer([208, 161, 208, 176, 208, 189, 208]))
parser.execute(new Buffer([186, 209, 130, 45, 208, 159, 208, 181, 209, 130]))
assert.strictEqual(replyCount, 2)
parser.execute(new Buffer('foo\r\n$5\r\nfoo\r\n\r\n'))
parser.execute(new Buffer('\r\n$5\r\nfoo\r\n\r\n'))
assert.strictEqual(replyCount, 4)
parser.execute(new Buffer('+foo\r'))
assert.strictEqual(replyCount, 4)
Expand Down

0 comments on commit 7b4dbf2

Please sign in to comment.