Skip to content

Commit

Permalink
string_decoder: fix bad utf8 character handling
Browse files Browse the repository at this point in the history
This commit fixes an issue when extra utf8 continuation bytes appear
at the end of a chunk of data, causing miscalculations to be made
when checking how many bytes are needed to decode a complete
character.

Fixes: #7308
  • Loading branch information
mscdex committed Jun 15, 2016
1 parent 1a1ff77 commit 52462c6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
21 changes: 18 additions & 3 deletions lib/string_decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,39 @@ function utf8CheckIncomplete(self, buf, i) {
return 0;
var nb = utf8CheckByte(buf[j--]);
if (nb >= 0) {
if (nb > 0)
if (nb > 0) {
self.lastNeed = nb + 1 - (buf.length - j);
if (self.lastNeed < 0) {
self.lastNeed = 0;
nb = 0;
}
}
return nb;
}
if (j < i)
return 0;
nb = utf8CheckByte(buf[j--]);
if (nb >= 0) {
if (nb > 0)
if (nb > 0) {
self.lastNeed = nb + 1 - (buf.length - j);
if (self.lastNeed < 0) {
self.lastNeed = 0;
nb = 0;
}
}
return nb;
}
if (j < i)
return 0;
nb = utf8CheckByte(buf[j--]);
if (nb >= 0) {
if (nb > 0)
if (nb > 0) {
self.lastNeed = nb + 1 - (buf.length - j);
if (self.lastNeed < 0) {
self.lastNeed = 0;
nb = 0;
}
}
return nb;
}
return 0;
Expand Down
7 changes: 6 additions & 1 deletion test/parallel/test-string-decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,14 @@ assert.strictEqual(decoder.write(Buffer.from('\ufffd\ufffd\ufffd')),
assert.strictEqual(decoder.end(), '');

decoder = new StringDecoder('utf8');
assert.strictEqual(decoder.write(Buffer.from('efbfbde2', 'hex')), '\ufffd');
assert.strictEqual(decoder.write(Buffer.from('EFBFBDE2', 'hex')), '\ufffd');
assert.strictEqual(decoder.end(), '\ufffd');

decoder = new StringDecoder('utf8');
assert.strictEqual(decoder.write(Buffer.from('C9B5A9', 'hex')), 'ɵ\ufffd');
assert.strictEqual(decoder.write(Buffer.from('41', 'hex')), 'A');
assert.strictEqual(decoder.end(), '');


// Additional UTF-16LE surrogate pair tests
decoder = new StringDecoder('utf16le');
Expand Down

0 comments on commit 52462c6

Please sign in to comment.