Skip to content

Commit

Permalink
lib: handle partial multibyte chars in last chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavnikolaj authored and bnoordhuis committed Sep 25, 2015
1 parent 9f1f7c5 commit a45d84c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/iconv.js
Expand Up @@ -96,6 +96,9 @@ function convert(input, context) {
if (!(input instanceof Buffer) && input !== FLUSH) {
throw new Error('Bad argument.'); // Not a buffer or a string.
}
if (context !== null && context.trailer !== null && input === FLUSH) {
throw errnoException('EINVAL', 'Incomplete character sequence.');
}
if (context !== null && context.trailer !== null) {
// Prepend input buffer with trailer from last chunk.
var newbuf = new Buffer(context.trailer.length + input.length);
Expand Down
27 changes: 27 additions & 0 deletions test/test-stream.js
Expand Up @@ -169,3 +169,30 @@ assert(new Iconv('ascii', 'ascii') instanceof stream.Stream);
stream.end('b2s=', 'base64');
assert(ok);
})();

(function() {
var ok = false;
var stream = Iconv('gb18030', 'utf-8');
stream.once('error', function(e) {
assert.equal(e.message, 'Incomplete character sequence.');
assert.equal(e.code, 'EINVAL');
ok = true;
});
var octets = [
0x00, 0xf1, 0x52, 0x00, 0x00, 0x78, 0x51, 0xd9, 0xf7, 0x78, 0x51, 0xd9
];
stream.end(new Buffer(octets));
assert(ok);
})();

(function() {
var ok = false;
var stream = Iconv('utf-8', 'utf-16');
stream.once('error', function(e) {
assert.equal(e.message, 'Incomplete character sequence.');
assert.equal(e.code, 'EINVAL');
ok = true;
});
stream.end(new Buffer([0xc3]));
assert(ok);
})();

0 comments on commit a45d84c

Please sign in to comment.