From e589834f136b3b6492700eacb460682de154fc11 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 24 May 2017 12:12:14 +0200 Subject: [PATCH] Ensure that `TilingPattern`s have valid (non-zero) /BBox arrays (issue 8330) Fixes 8330. --- src/core/evaluator.js | 36 ++++++++++++++++++++++------------- src/core/pattern.js | 18 ++++++++++++------ src/display/pattern_helper.js | 2 +- test/pdfs/issue8330.pdf.link | 1 + test/test_manifest.json | 8 ++++++++ 5 files changed, 45 insertions(+), 20 deletions(-) create mode 100644 test/pdfs/issue8330.pdf.link diff --git a/src/core/evaluator.js b/src/core/evaluator.js index dcd3594b6..41f0c636d 100644 --- a/src/core/evaluator.js +++ b/src/core/evaluator.js @@ -480,30 +480,40 @@ var PartialEvaluator = (function PartialEvaluatorClosure() { stateManager.state.clone()); }, - handleTilingType: - function PartialEvaluator_handleTilingType(fn, args, resources, - pattern, patternDict, - operatorList, task) { + handleTilingType(fn, args, resources, pattern, patternDict, operatorList, + task) { // Create an IR of the pattern code. - var tilingOpList = new OperatorList(); + let tilingOpList = new OperatorList(); // Merge the available resources, to prevent issues when the patternDict // is missing some /Resources entries (fixes issue6541.pdf). - var resourcesArray = [patternDict.get('Resources'), resources]; - var patternResources = Dict.merge(this.xref, resourcesArray); + let resourcesArray = [patternDict.get('Resources'), resources]; + let patternResources = Dict.merge(this.xref, resourcesArray); return this.getOperatorList({ stream: pattern, task, resources: patternResources, operatorList: tilingOpList, - }).then(function () { - // Add the dependencies to the parent operator list so they are - // resolved before sub operator list is executed synchronously. - operatorList.addDependencies(tilingOpList.dependencies); - operatorList.addOp(fn, getTilingPatternIR({ + }).then(function() { + return getTilingPatternIR({ fnArray: tilingOpList.fnArray, argsArray: tilingOpList.argsArray, - }, patternDict, args)); + }, patternDict, args); + }).then(function(tilingPatternIR) { + // Add the dependencies to the parent operator list so they are + // resolved before the sub operator list is executed synchronously. + operatorList.addDependencies(tilingOpList.dependencies); + operatorList.addOp(fn, tilingPatternIR); + }, (reason) => { + if (this.options.ignoreErrors) { + // Error(s) in the TilingPattern -- sending unsupported feature + // notification and allow rendering to continue. + this.handler.send('UnsupportedFeature', + { featureId: UNSUPPORTED_FEATURES.unknown, }); + warn(`handleTilingType - ignoring pattern: "${reason}".`); + return; + } + throw reason; }); }, diff --git a/src/core/pattern.js b/src/core/pattern.js index 7a75e924a..ccaa4b8e8 100644 --- a/src/core/pattern.js +++ b/src/core/pattern.js @@ -795,12 +795,18 @@ Shadings.Dummy = (function DummyClosure() { })(); function getTilingPatternIR(operatorList, dict, args) { - var matrix = dict.getArray('Matrix'); - var bbox = dict.getArray('BBox'); - var xstep = dict.get('XStep'); - var ystep = dict.get('YStep'); - var paintType = dict.get('PaintType'); - var tilingType = dict.get('TilingType'); + let matrix = dict.getArray('Matrix'); + let bbox = Util.normalizeRect(dict.getArray('BBox')); + let xstep = dict.get('XStep'); + let ystep = dict.get('YStep'); + let paintType = dict.get('PaintType'); + let tilingType = dict.get('TilingType'); + + // Ensure that the pattern has a non-zero width and height, to prevent errors + // in `pattern_helper.js` (fixes issue8330.pdf). + if ((bbox[2] - bbox[0]) === 0 || (bbox[3] - bbox[1]) === 0) { + throw new Error(`getTilingPatternIR - invalid /BBox array: [${bbox}].`); + } return [ 'TilingPattern', args, operatorList, matrix, bbox, xstep, ystep, diff --git a/src/display/pattern_helper.js b/src/display/pattern_helper.js index dc280f271..97256eafc 100644 --- a/src/display/pattern_helper.js +++ b/src/display/pattern_helper.js @@ -304,7 +304,7 @@ var TilingPattern = (function TilingPatternClosure() { function TilingPattern(IR, color, ctx, canvasGraphicsFactory, baseTransform) { this.operatorList = IR[2]; this.matrix = IR[3] || [1, 0, 0, 1, 0, 0]; - this.bbox = Util.normalizeRect(IR[4]); + this.bbox = IR[4]; this.xstep = IR[5]; this.ystep = IR[6]; this.paintType = IR[7]; diff --git a/test/pdfs/issue8330.pdf.link b/test/pdfs/issue8330.pdf.link new file mode 100644 index 000000000..9e115139d --- /dev/null +++ b/test/pdfs/issue8330.pdf.link @@ -0,0 +1 @@ +https://github.com/mozilla/pdf.js/files/954710/FailingPDF.pdf diff --git a/test/test_manifest.json b/test/test_manifest.json index 2b07ae514..2241eae98 100644 --- a/test/test_manifest.json +++ b/test/test_manifest.json @@ -1269,6 +1269,14 @@ "rounds": 1, "type": "eq" }, + { "id": "issue8330", + "file": "pdfs/issue8330.pdf", + "md5": "9010093d07dd62d3b49378fd48cf45f9", + "rounds": 1, + "lastPage": 1, + "link": true, + "type": "eq" + }, { "id": "issue4573", "file": "pdfs/issue4573.pdf", "md5": "34b0c4fdee19e57033275b766c5f57a3",