From fa69ff773a18c599ddd95ee2d246cac7ac529521 Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Fri, 18 Jan 2019 19:25:55 +0000 Subject: [PATCH] Input image decoding fail fast by default --- docs/api-constructor.md | 5 ++--- docs/changelog.md | 4 ++++ lib/constructor.js | 5 ++--- lib/input.js | 2 +- src/common.h | 2 +- test/unit/failOnError.js | 22 ++++++++++------------ 6 files changed, 20 insertions(+), 20 deletions(-) diff --git a/docs/api-constructor.md b/docs/api-constructor.md index 94d917ff..263118d9 100644 --- a/docs/api-constructor.md +++ b/docs/api-constructor.md @@ -9,9 +9,8 @@ a String containing the path to an JPEG, PNG, WebP, GIF, SVG or TIFF image file. JPEG, PNG, WebP, GIF, SVG, TIFF or raw pixel image data can be streamed into the object when not present. - `options` **[Object][3]?** if present, is an Object with optional attributes. - - `options.failOnError` **[Boolean][4]** by default apply a "best effort" - to decode images, even if the data is corrupt or invalid. Set this flag to true - if you'd rather halt processing and raise an error when loading invalid images. (optional, default `false`) + - `options.failOnError` **[Boolean][4]** by default halt processing and raise an error when loading invalid images. + Set this flag to `false` if you'd rather apply a "best effort" to decode images, even if the data is corrupt or invalid. (optional, default `true`) - `options.density` **[Number][5]** number representing the DPI for vector images. (optional, default `72`) - `options.page` **[Number][5]** page number to extract for multi-page input (GIF, TIFF) (optional, default `0`) - `options.raw` **[Object][3]?** describes raw pixel input image data. See `raw()` for pixel ordering. diff --git a/docs/changelog.md b/docs/changelog.md index 04dde5e9..6374d80c 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -4,6 +4,10 @@ Requires libvips v8.7.0. +#### v0.21.3 - TBD + +* Input image decoding now fails fast, set `failOnError` to change this behaviour. + #### v0.21.2 - 13th January 2019 * Ensure all metadata is removed from PNG output unless `withMetadata` used. diff --git a/lib/constructor.js b/lib/constructor.js index 303c4319..442594f4 100644 --- a/lib/constructor.js +++ b/lib/constructor.js @@ -61,9 +61,8 @@ const debuglog = util.debuglog('sharp'); * a String containing the path to an JPEG, PNG, WebP, GIF, SVG or TIFF image file. * JPEG, PNG, WebP, GIF, SVG, TIFF or raw pixel image data can be streamed into the object when not present. * @param {Object} [options] - if present, is an Object with optional attributes. - * @param {Boolean} [options.failOnError=false] - by default apply a "best effort" - * to decode images, even if the data is corrupt or invalid. Set this flag to true - * if you'd rather halt processing and raise an error when loading invalid images. + * @param {Boolean} [options.failOnError=true] - by default halt processing and raise an error when loading invalid images. + * Set this flag to `false` if you'd rather apply a "best effort" to decode images, even if the data is corrupt or invalid. * @param {Number} [options.density=72] - number representing the DPI for vector images. * @param {Number} [options.page=0] - page number to extract for multi-page input (GIF, TIFF) * @param {Object} [options.raw] - describes raw pixel input image data. See `raw()` for pixel ordering. diff --git a/lib/input.js b/lib/input.js index ef8afc5a..74831210 100644 --- a/lib/input.js +++ b/lib/input.js @@ -9,7 +9,7 @@ const sharp = require('../build/Release/sharp.node'); * @private */ function _createInputDescriptor (input, inputOptions, containerOptions) { - const inputDescriptor = { failOnError: false }; + const inputDescriptor = { failOnError: true }; if (is.string(input)) { // filesystem inputDescriptor.file = input; diff --git a/src/common.h b/src/common.h index 04d9dd0f..91e1bd9b 100644 --- a/src/common.h +++ b/src/common.h @@ -61,7 +61,7 @@ namespace sharp { InputDescriptor(): buffer(nullptr), - failOnError(FALSE), + failOnError(TRUE), bufferLength(0), density(72.0), rawChannels(0), diff --git a/test/unit/failOnError.js b/test/unit/failOnError.js index 6c607b83..c30afc94 100644 --- a/test/unit/failOnError.js +++ b/test/unit/failOnError.js @@ -6,10 +6,9 @@ const sharp = require('../../'); const fixtures = require('../fixtures'); describe('failOnError', function () { - it('handles truncated JPEG by default', function (done) { - sharp(fixtures.inputJpgTruncated) + it('handles truncated JPEG', function (done) { + sharp(fixtures.inputJpgTruncated, { failOnError: false }) .resize(320, 240) - // .toFile(fixtures.expected('truncated.jpg'), done); .toBuffer(function (err, data, info) { if (err) throw err; assert.strictEqual('jpeg', info.format); @@ -19,10 +18,9 @@ describe('failOnError', function () { }); }); - it('handles truncated PNG by default', function (done) { - sharp(fixtures.inputPngTruncated) + it('handles truncated PNG', function (done) { + sharp(fixtures.inputPngTruncated, { failOnError: false }) .resize(320, 240) - // .toFile(fixtures.expected('truncated.png'), done); .toBuffer(function (err, data, info) { if (err) throw err; assert.strictEqual('png', info.format); @@ -46,8 +44,8 @@ describe('failOnError', function () { }); }); - it('returns errors to callback for truncated JPEG when failOnError is set', function (done) { - sharp(fixtures.inputJpgTruncated, { failOnError: true }).toBuffer(function (err, data, info) { + it('returns errors to callback for truncated JPEG', function (done) { + sharp(fixtures.inputJpgTruncated).toBuffer(function (err, data, info) { assert.ok(err.message.includes('VipsJpeg: Premature end of JPEG file'), err); assert.strictEqual(data, null); assert.strictEqual(info, null); @@ -55,8 +53,8 @@ describe('failOnError', function () { }); }); - it('returns errors to callback for truncated PNG when failOnError is set', function (done) { - sharp(fixtures.inputPngTruncated, { failOnError: true }).toBuffer(function (err, data, info) { + it('returns errors to callback for truncated PNG', function (done) { + sharp(fixtures.inputPngTruncated).toBuffer(function (err, data, info) { assert.ok(err.message.includes('vipspng: libpng read error'), err); assert.strictEqual(data, null); assert.strictEqual(info, null); @@ -64,8 +62,8 @@ describe('failOnError', function () { }); }); - it('rejects promises for truncated JPEG when failOnError is set', function (done) { - sharp(fixtures.inputJpgTruncated, { failOnError: true }) + it('rejects promises for truncated JPEG', function (done) { + sharp(fixtures.inputJpgTruncated) .toBuffer() .then(() => { throw new Error('Expected rejection');