Skip to content

Commit 336a74a

Browse files
Merge pull request #13796 from Snuffleupagus/issue-13794
Allow `StreamsSequenceStream.readBlock` to skip sub-streams with errors (issue 13794)
2 parents 45f3804 + 885e7a8 commit 336a74a

File tree

5 files changed

+46
-6
lines changed

5 files changed

+46
-6
lines changed

src/core/decode_stream.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class DecodeStream extends BaseStream {
127127
}
128128

129129
class StreamsSequenceStream extends DecodeStream {
130-
constructor(streams) {
130+
constructor(streams, onError = null) {
131131
let maybeLength = 0;
132132
for (const stream of streams) {
133133
maybeLength +=
@@ -138,6 +138,7 @@ class StreamsSequenceStream extends DecodeStream {
138138
super(maybeLength);
139139

140140
this.streams = streams;
141+
this._onError = onError;
141142
}
142143

143144
readBlock() {
@@ -147,7 +148,16 @@ class StreamsSequenceStream extends DecodeStream {
147148
return;
148149
}
149150
const stream = streams.shift();
150-
const chunk = stream.getBytes();
151+
let chunk;
152+
try {
153+
chunk = stream.getBytes();
154+
} catch (reason) {
155+
if (this._onError) {
156+
this._onError(reason, stream.dict && stream.dict.objId);
157+
return;
158+
}
159+
throw reason;
160+
}
151161
const bufferLength = this.bufferLength;
152162
const newLength = bufferLength + chunk.length;
153163
const buffer = this.ensureBuffer(newLength);

src/core/document.js

+24-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
stringToPDFString,
3131
stringToUTF8String,
3232
unreachable,
33+
UNSUPPORTED_FEATURES,
3334
Util,
3435
warn,
3536
} from "../shared/util.js";
@@ -225,16 +226,35 @@ class Page {
225226
return shadow(this, "rotate", rotate);
226227
}
227228

229+
/**
230+
* @private
231+
*/
232+
_onSubStreamError(handler, reason, objId) {
233+
if (this.evaluatorOptions.ignoreErrors) {
234+
// Error(s) when reading one of the /Contents sub-streams -- sending
235+
// unsupported feature notification and allow parsing to continue.
236+
handler.send("UnsupportedFeature", {
237+
featureId: UNSUPPORTED_FEATURES.errorContentSubStream,
238+
});
239+
warn(`getContentStream - ignoring sub-stream (${objId}): "${reason}".`);
240+
return;
241+
}
242+
throw reason;
243+
}
244+
228245
/**
229246
* @returns {Promise<BaseStream>}
230247
*/
231-
getContentStream() {
248+
getContentStream(handler) {
232249
return this.pdfManager.ensure(this, "content").then(content => {
233250
if (content instanceof BaseStream) {
234251
return content;
235252
}
236253
if (Array.isArray(content)) {
237-
return new StreamsSequenceStream(content);
254+
return new StreamsSequenceStream(
255+
content,
256+
this._onSubStreamError.bind(this, handler)
257+
);
238258
}
239259
// Replace non-existent page content with empty content.
240260
return new NullStream();
@@ -307,7 +327,7 @@ class Page {
307327
renderInteractiveForms,
308328
annotationStorage,
309329
}) {
310-
const contentStreamPromise = this.getContentStream();
330+
const contentStreamPromise = this.getContentStream(handler);
311331
const resourcesPromise = this.loadResources([
312332
"ColorSpace",
313333
"ExtGState",
@@ -417,7 +437,7 @@ class Page {
417437
sink,
418438
combineTextItems,
419439
}) {
420-
const contentStreamPromise = this.getContentStream();
440+
const contentStreamPromise = this.getContentStream(handler);
421441
const resourcesPromise = this.loadResources([
422442
"ExtGState",
423443
"Font",

src/shared/util.js

+1
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ const UNSUPPORTED_FEATURES = {
335335
errorFontBuildPath: "errorFontBuildPath",
336336
errorFontGetPath: "errorFontGetPath",
337337
errorMarkedContent: "errorMarkedContent",
338+
errorContentSubStream: "errorContentSubStream",
338339
};
339340

340341
const PasswordResponses = {

test/pdfs/issue13794.pdf.link

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://github.com/mozilla/pdf.js/files/6876708/Scan-to-Mail-PDF1_.1.pdf

test/test_manifest.json

+8
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,14 @@
13991399
"lastPage": 7,
14001400
"type": "eq"
14011401
},
1402+
{ "id": "issue13794",
1403+
"file": "pdfs/issue13794.pdf",
1404+
"md5": "6b4c099e04c9df145198740f2bf75c48",
1405+
"link": true,
1406+
"rounds": 1,
1407+
"firstPage": 3,
1408+
"type": "eq"
1409+
},
14021410
{ "id": "issue9262",
14031411
"file": "pdfs/issue9262_reduced.pdf",
14041412
"md5": "5347ce2d7b3866625c22e115fd90e0de",

0 commit comments

Comments
 (0)