Skip to content

Allow StreamsSequenceStream.readBlock to skip sub-streams with errors (issue 13794) #13796

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

Merged
merged 1 commit into from
Jul 27, 2021
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
14 changes: 12 additions & 2 deletions src/core/decode_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class DecodeStream extends BaseStream {
}

class StreamsSequenceStream extends DecodeStream {
constructor(streams) {
constructor(streams, onError = null) {
let maybeLength = 0;
for (const stream of streams) {
maybeLength +=
Expand All @@ -138,6 +138,7 @@ class StreamsSequenceStream extends DecodeStream {
super(maybeLength);

this.streams = streams;
this._onError = onError;
}

readBlock() {
Expand All @@ -147,7 +148,16 @@ class StreamsSequenceStream extends DecodeStream {
return;
}
const stream = streams.shift();
const chunk = stream.getBytes();
let chunk;
try {
chunk = stream.getBytes();
} catch (reason) {
if (this._onError) {
this._onError(reason, stream.dict && stream.dict.objId);
return;
}
throw reason;
}
const bufferLength = this.bufferLength;
const newLength = bufferLength + chunk.length;
const buffer = this.ensureBuffer(newLength);
Expand Down
28 changes: 24 additions & 4 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
stringToPDFString,
stringToUTF8String,
unreachable,
UNSUPPORTED_FEATURES,
Util,
warn,
} from "../shared/util.js";
Expand Down Expand Up @@ -225,16 +226,35 @@ class Page {
return shadow(this, "rotate", rotate);
}

/**
* @private
*/
_onSubStreamError(handler, reason, objId) {
if (this.evaluatorOptions.ignoreErrors) {
// Error(s) when reading one of the /Contents sub-streams -- sending
// unsupported feature notification and allow parsing to continue.
handler.send("UnsupportedFeature", {
featureId: UNSUPPORTED_FEATURES.errorContentSubStream,
});
warn(`getContentStream - ignoring sub-stream (${objId}): "${reason}".`);
return;
}
throw reason;
}

/**
* @returns {Promise<BaseStream>}
*/
getContentStream() {
getContentStream(handler) {
return this.pdfManager.ensure(this, "content").then(content => {
if (content instanceof BaseStream) {
return content;
}
if (Array.isArray(content)) {
return new StreamsSequenceStream(content);
return new StreamsSequenceStream(
content,
this._onSubStreamError.bind(this, handler)
);
}
// Replace non-existent page content with empty content.
return new NullStream();
Expand Down Expand Up @@ -307,7 +327,7 @@ class Page {
renderInteractiveForms,
annotationStorage,
}) {
const contentStreamPromise = this.getContentStream();
const contentStreamPromise = this.getContentStream(handler);
const resourcesPromise = this.loadResources([
"ColorSpace",
"ExtGState",
Expand Down Expand Up @@ -417,7 +437,7 @@ class Page {
sink,
combineTextItems,
}) {
const contentStreamPromise = this.getContentStream();
const contentStreamPromise = this.getContentStream(handler);
const resourcesPromise = this.loadResources([
"ExtGState",
"Font",
Expand Down
1 change: 1 addition & 0 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ const UNSUPPORTED_FEATURES = {
errorFontBuildPath: "errorFontBuildPath",
errorFontGetPath: "errorFontGetPath",
errorMarkedContent: "errorMarkedContent",
errorContentSubStream: "errorContentSubStream",
};

const PasswordResponses = {
Expand Down
1 change: 1 addition & 0 deletions test/pdfs/issue13794.pdf.link
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/mozilla/pdf.js/files/6876708/Scan-to-Mail-PDF1_.1.pdf
8 changes: 8 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,14 @@
"lastPage": 7,
"type": "eq"
},
{ "id": "issue13794",
"file": "pdfs/issue13794.pdf",
"md5": "6b4c099e04c9df145198740f2bf75c48",
"link": true,
"rounds": 1,
"firstPage": 3,
"type": "eq"
},
{ "id": "issue9262",
"file": "pdfs/issue9262_reduced.pdf",
"md5": "5347ce2d7b3866625c22e115fd90e0de",
Expand Down