From 2914659815868a9a5a1ddcee02f1bc7dead59101 Mon Sep 17 00:00:00 2001 From: Luke Page Date: Mon, 3 Aug 2015 16:35:05 +0100 Subject: [PATCH] Do not process if checksum errors --- lib/parser-async.js | 20 ++++++++++++++++---- test/convert-images.js | 8 ++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/parser-async.js b/lib/parser-async.js index 7561747..b5a9ef2 100644 --- a/lib/parser-async.js +++ b/lib/parser-async.js @@ -34,7 +34,7 @@ var ParserAsync = module.exports = function(options) { this._parser = new Parser(options, { read: this.read.bind(this), - error: this.emit.bind(this, "error"), + error: this._handleError.bind(this), metadata: this.emit.bind(this, "metadata"), gamma: this.emit.bind(this, "gamma"), finished: this._finished.bind(this), @@ -44,13 +44,14 @@ var ParserAsync = module.exports = function(options) { this._options = options; this.writable = true; - this.on('error', this._handleError.bind(this)); this._parser.start(); }; util.inherits(ParserAsync, ChunkStream); -ParserAsync.prototype._handleError = function() { +ParserAsync.prototype._handleError = function(err) { + + this.emit('error', err); this.writable = false; @@ -59,6 +60,8 @@ ParserAsync.prototype._handleError = function() { if (this._inflate && this._inflate.destroy) { this._inflate.destroy(); } + + this.errord = true; }; ParserAsync.prototype._inflateData = function(data) { @@ -89,6 +92,10 @@ ParserAsync.prototype._createData = function(width, height, bpp, depth, interlac }; ParserAsync.prototype._finished = function(data) { + if (this.errord) { + return; + } + if (!this._inflate) { this.emit('error', 'No Inflate block'); } else { @@ -100,6 +107,10 @@ ParserAsync.prototype._finished = function(data) { ParserAsync.prototype._complete = function(data, width, height) { + if (this.errord) { + return; + } + try { data = bitmapper.dataToBitMap(data, width, height, this._bpp, @@ -109,7 +120,8 @@ ParserAsync.prototype._complete = function(data, width, height) { data = this._parser.reverseFiltered(data, this._depth, width, height); } catch(e) { - this.emit('error', e); + this._handleError(); + return; } this.emit('parsed', data); diff --git a/test/convert-images.js b/test/convert-images.js index e009573..4c56aa5 100644 --- a/test/convert-images.js +++ b/test/convert-images.js @@ -35,7 +35,7 @@ module.exports = function(done) { var png = PNG.sync.read(data); } catch (e) { if (!expectedError) { - console.log("Unexpected error parsing.." + file); + console.log("Sync: Unexpected error parsing.." + file); console.log(e); console.log(e.stack); } @@ -45,7 +45,7 @@ module.exports = function(done) { if (!syncError) { if (expectedError) { - console.log("Error expected, parsed fine ..", file); + console.log("Sync: Error expected, parsed fine ..", file); complete(); } else { @@ -66,14 +66,14 @@ module.exports = function(done) { .pipe(new PNG()) .on('error', function (err) { if (!expectedError) { - console.log("Error reading " + file, err); + console.log("Async: Unexpected error parsing.." + file, err); } complete(); }) .on('parsed', function () { if (expectedError) { - console.log("Error expected, parsed fine", file); + console.log("Async: Error expected, parsed fine ..", file); } //this.adjustGamma();