Skip to content

Commit

Permalink
lib: fix issue with invalid input
Browse files Browse the repository at this point in the history
Fixes: #53
  • Loading branch information
addaleax committed Feb 21, 2018
1 parent 26eb601 commit 5f64041
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 30 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class JSLzmaStream extends stream.Transform {
}

_transform(chunk, encoding, callback) {
if (!this.nativeStream) return;
// Split the chunk at 'YZ'. This is used to have a clean boundary at the
// end of each `.xz` file stream.
var possibleEndIndex = bufferIndexOfYZ(chunk);
Expand Down
Binary file added test/invalid.xz
Binary file not shown.
60 changes: 30 additions & 30 deletions test/parse-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ describe('lzma', function() {
assert.ok(info.fileSize < info.uncompressedSize);
assert.deepStrictEqual(info.checks, [ lzma.CHECK_CRC64 ]);
};

if (typeof gc !== 'undefined') {
afterEach('garbage-collect', gc);
}

describe('#parseFileIndex', function() {
var hamletXZ;
before('Read from a buffer into memory', function() {
hamletXZ = fs.readFileSync('test/hamlet.txt.2stream.xz');
});

it('should fail for zero-length files', function(done) {
lzma.parseFileIndex({
fileSize: 0,
Expand All @@ -37,11 +37,11 @@ describe('lzma', function() {
}, function(err, info) {
assert.ok(err);
assert(/File is empty/.test(err.message));

done();
});
});

it('should fail for too-small files', function(done) {
lzma.parseFileIndex({
fileSize: 10,
Expand All @@ -51,11 +51,11 @@ describe('lzma', function() {
}, function(err, info) {
assert.ok(err);
assert(/Too small/.test(err.message));

done();
});
});

it('should fail for truncated files', function(done) {
lzma.parseFileIndex({
fileSize: hamletXZ.length,
Expand All @@ -65,11 +65,11 @@ describe('lzma', function() {
}, function(err, info) {
assert.ok(err);
assert.strictEqual(err.name, 'LZMA_DATA_ERROR');

done();
});
});

it('should fail when I/O errors are passed along', function(done) {
lzma.parseFileIndex({
fileSize: hamletXZ.length,
Expand All @@ -79,11 +79,11 @@ describe('lzma', function() {
}, function(err, info) {
assert.ok(err);
assert.strictEqual(err.message, 'I/O failed');

done();
});
});

it('should fail when I/O errors are passed along, sync version', function() {
assert.throws(function() {
lzma.parseFileIndex({
Expand All @@ -94,7 +94,7 @@ describe('lzma', function() {
});
}, /I\/O failed/);
});

it('should fail for invalid files', function(done) {
lzma.parseFileIndex({
fileSize: hamletXZ.length,
Expand All @@ -106,11 +106,11 @@ describe('lzma', function() {
}, function(err, info) {
assert.ok(err);
assert.strictEqual(err.name, 'LZMA_DATA_ERROR');

done();
});
});

it('should be able to parse a file synchronously', function(done) {
lzma.parseFileIndex({
fileSize: hamletXZ.length,
Expand All @@ -119,24 +119,24 @@ describe('lzma', function() {
}
}, function(err, info) {
if (err) return done(err);

checkInfo(info);

done();
});
});

it('should be able to parse a file with synchronous return', function() {
var info = lzma.parseFileIndex({
fileSize: hamletXZ.length,
read: function(count, offset, cb) {
cb(hamletXZ.slice(offset, offset + count));
}
});

checkInfo(info);
});

it('should be able to parse a file asynchronously', function(done) {
lzma.parseFileIndex({
fileSize: hamletXZ.length,
Expand All @@ -147,13 +147,13 @@ describe('lzma', function() {
}
}, function(err, info) {
if (err) return done(err);

checkInfo(info);

done();
});
});

it('should be able to parse a file asynchronously, alternative callback style', function(done) {
lzma.parseFileIndex({
fileSize: hamletXZ.length,
Expand All @@ -164,14 +164,14 @@ describe('lzma', function() {
}
}, function(err, info) {
if (err) return done(err);

checkInfo(info);

done();
});
});
});

describe('#parseFileIndexFD', function() {
var fd;
before('Open the file', function(done) {
Expand All @@ -180,22 +180,22 @@ describe('lzma', function() {
done(err);
});
});

after('Close the file', function(done) {
fs.close(fd, done);
});

it('should be able to parse a file from a file descriptor', function(done) {
lzma.parseFileIndexFD(fd, function(err, info) {
if (err) return done(err);

checkInfo(info);
done();
});
});

it('should fail for invalid file descriptors', function(done) {
lzma.parseFileIndexFD(-1, function(err, info) {
lzma.parseFileIndexFD(1000, function(err, info) {
assert.ok(err);
assert.ok(!info);
done();
Expand Down
17 changes: 17 additions & 0 deletions test/regression-53.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

var lzma = require('../');
var fs = require('fs');
var assert = require('assert');

describe('regression-#53', function() {
it('should perform correctly', function(done) {
var input = fs.readFileSync('test/invalid.xz');

lzma.decompress(input, function(result, error) {
assert.strictEqual(result, null);
assert.strictEqual(error.name, 'LZMA_DATA_ERROR');
done();
});
});
});

0 comments on commit 5f64041

Please sign in to comment.