diff --git a/src/core/chunked_stream.js b/src/core/chunked_stream.js index 688267072..426b7abb6 100644 --- a/src/core/chunked_stream.js +++ b/src/core/chunked_stream.js @@ -107,6 +107,9 @@ class ChunkedStream extends Stream { } const chunk = Math.floor(pos / this.chunkSize); + if (chunk > this.numChunks) { + return; + } if (chunk === this.lastSuccessfulEnsureByteChunk) { return; } @@ -125,9 +128,14 @@ class ChunkedStream extends Stream { return; } - const chunkSize = this.chunkSize; - const beginChunk = Math.floor(begin / chunkSize); - const endChunk = Math.floor((end - 1) / chunkSize) + 1; + const beginChunk = Math.floor(begin / this.chunkSize); + if (beginChunk > this.numChunks) { + return; + } + const endChunk = Math.min( + Math.floor((end - 1) / this.chunkSize) + 1, + this.numChunks + ); for (let chunk = beginChunk; chunk < endChunk; ++chunk) { if (!this._loadedChunks.has(chunk)) { throw new MissingDataException(begin, end); diff --git a/test/pdfs/.gitignore b/test/pdfs/.gitignore index a957af612..1b7bd6cee 100644 --- a/test/pdfs/.gitignore +++ b/test/pdfs/.gitignore @@ -486,4 +486,5 @@ !pr12828.pdf !secHandler.pdf !rc_annotation.pdf -!issue14267.pdf \ No newline at end of file +!issue14267.pdf +!PDFBOX-4352-0.pdf diff --git a/test/pdfs/PDFBOX-4352-0.pdf b/test/pdfs/PDFBOX-4352-0.pdf new file mode 100644 index 000000000..12b1ef147 Binary files /dev/null and b/test/pdfs/PDFBOX-4352-0.pdf differ diff --git a/test/unit/api_spec.js b/test/unit/api_spec.js index b41a0f91d..9a5428bb1 100644 --- a/test/unit/api_spec.js +++ b/test/unit/api_spec.js @@ -443,6 +443,21 @@ describe("api", function () { await Promise.all([loadingTask1.destroy(), loadingTask2.destroy()]); }); + + it("creates pdf doc from PDF file with bad XRef table", async function () { + // A corrupt PDF file, where the XRef table have (some) bogus entries. + const loadingTask = getDocument( + buildGetDocumentParams("PDFBOX-4352-0.pdf", { + rangeChunkSize: 100, + }) + ); + expect(loadingTask instanceof PDFDocumentLoadingTask).toEqual(true); + + const pdfDocument = await loadingTask.promise; + expect(pdfDocument.numPages).toEqual(1); + + await loadingTask.destroy(); + }); }); describe("PDFWorker", function () {