Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to handle corrupt PDF documents that contains path operators inside of text object (issue 10542) #10756

Merged
merged 1 commit into from
May 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -822,14 +822,30 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
return fontCapability.promise;
},

buildPath: function PartialEvaluator_buildPath(operatorList, fn, args) {
buildPath(operatorList, fn, args, parsingText = false) {
var lastIndex = operatorList.length - 1;
if (!args) {
args = [];
}
if (lastIndex < 0 ||
operatorList.fnArray[lastIndex] !== OPS.constructPath) {
// Handle corrupt PDF documents that contains path operators inside of
// text objects, which may shift subsequent text, by enclosing the path
// operator in save/restore operators (fixes issue10542_reduced.pdf).
//
// Note that this will effectively disable the optimization in the
// `else` branch below, but given that this type of corruption is
// *extremely* rare that shouldn't really matter much in practice.
if (parsingText) {
Copy link
Collaborator Author

@Snuffleupagus Snuffleupagus Apr 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before adding the test-case, I did a SKIP_BABEL=true gulp test run with assert(!parsingText); added just above this line. Since there wasn't any test failures, that made me a bit less uneasy about the general approach of this patch.

warn(`Encountered path operator "${fn}" inside of a text object.`);
operatorList.addOp(OPS.save, null);
}

operatorList.addOp(OPS.constructPath, [[fn], args]);

if (parsingText) {
operatorList.addOp(OPS.restore, null);
}
} else {
var opArgs = operatorList.argsArray[lastIndex];
opArgs[0].push(fn);
Expand Down Expand Up @@ -881,6 +897,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {

var self = this;
var xref = this.xref;
let parsingText = false;
var imageCache = Object.create(null);

var xobjs = (resources.get('XObject') || Dict.empty);
Expand Down Expand Up @@ -999,6 +1016,12 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
operatorList.addOp(OPS.setFont, [loadedName, fontSize]);
}));
return;
case OPS.beginText:
parsingText = true;
break;
case OPS.endText:
parsingText = false;
break;
case OPS.endInlineImage:
var cacheKey = args[0].cacheKey;
if (cacheKey) {
Expand Down Expand Up @@ -1158,10 +1181,8 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
case OPS.curveTo2:
case OPS.curveTo3:
case OPS.closePath:
self.buildPath(operatorList, fn, args);
continue;
case OPS.rectangle:
self.buildPath(operatorList, fn, args);
self.buildPath(operatorList, fn, args, parsingText);
continue;
case OPS.markPoint:
case OPS.markPointProps:
Expand Down
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
!issue10388_reduced.pdf
!issue10438_reduced.pdf
!issue10529.pdf
!issue10542_reduced.pdf
!issue10665_reduced.pdf
!bad-PageLabels.pdf
!decodeACSuccessive.pdf
Expand Down
Binary file added test/pdfs/issue10542_reduced.pdf
Binary file not shown.
7 changes: 7 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,13 @@
"firstPage": 2,
"type": "eq"
},
{ "id": "issue10542",
"file": "pdfs/issue10542_reduced.pdf",
"md5": "92406cb903be6c7a63221ba61fcb8eaf",
"rounds": 1,
"link": false,
"type": "eq"
},
{ "id": "issue6289",
"file": "pdfs/issue6289.pdf",
"md5": "0869f3d147c734ec484ffd492104095d",
Expand Down