From 52ea20eda4b52286442635c3c8481fa382fe46ee Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Sun, 26 Nov 2023 17:32:52 +0100 Subject: [PATCH] Don't throw when there isn't enough data to get block info in flate stream but just ends the stream. --- src/core/flate_stream.js | 19 ++++++++++++++----- test/pdfs/issue17332.pdf.link | 1 + test/test_manifest.json | 9 +++++++++ 3 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 test/pdfs/issue17332.pdf.link diff --git a/src/core/flate_stream.js b/src/core/flate_stream.js index da8247aef..f2df7a17c 100644 --- a/src/core/flate_stream.js +++ b/src/core/flate_stream.js @@ -19,8 +19,8 @@ * license. */ +import { FormatError, info } from "../shared/util.js"; import { DecodeStream } from "./decode_stream.js"; -import { FormatError } from "../shared/util.js"; const codeLenCodeMap = new Int32Array([ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15, @@ -238,6 +238,11 @@ class FlateStream extends DecodeStream { return [codes, maxLen]; } + #endsStreamOnError(err) { + info(err); + this.eof = true; + } + readBlock() { let buffer, len; const str = this.str; @@ -253,19 +258,23 @@ class FlateStream extends DecodeStream { let b; if ((b = str.getByte()) === -1) { - throw new FormatError("Bad block header in flate stream"); + this.#endsStreamOnError("Bad block header in flate stream"); + return; } let blockLen = b; if ((b = str.getByte()) === -1) { - throw new FormatError("Bad block header in flate stream"); + this.#endsStreamOnError("Bad block header in flate stream"); + return; } blockLen |= b << 8; if ((b = str.getByte()) === -1) { - throw new FormatError("Bad block header in flate stream"); + this.#endsStreamOnError("Bad block header in flate stream"); + return; } let check = b; if ((b = str.getByte()) === -1) { - throw new FormatError("Bad block header in flate stream"); + this.#endsStreamOnError("Bad block header in flate stream"); + return; } check |= b << 8; if (check !== (~blockLen & 0xffff) && (blockLen !== 0 || check !== 0)) { diff --git a/test/pdfs/issue17332.pdf.link b/test/pdfs/issue17332.pdf.link new file mode 100644 index 000000000..d351c4d38 --- /dev/null +++ b/test/pdfs/issue17332.pdf.link @@ -0,0 +1 @@ +https://github.com/mozilla/pdf.js/files/13463186/test.1.pdf diff --git a/test/test_manifest.json b/test/test_manifest.json index 372298a51..de05cc03e 100644 --- a/test/test_manifest.json +++ b/test/test_manifest.json @@ -8372,5 +8372,14 @@ "lastPage": 2, "type": "eq", "forms": true + }, + { + "id": "issue17332-annotations", + "file": "pdfs/issue17332.pdf", + "md5": "140c34d900b797e1310b1cb142a4b8e3", + "rounds": 1, + "link": true, + "type": "eq", + "annotations": true } ]