Skip to content

Commit 5cbaa0f

Browse files
authored
fix: break infinite loop from invalid stream, add i306_err_invalid.pdf to verify fix of #306 (#379)
1 parent 7721899 commit 5cbaa0f

4 files changed

Lines changed: 9 additions & 6 deletions

File tree

base/core/parser.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ var Parser = (function ParserClosure() {
230230
while (stream.pos < stream.end) {
231231
var scanBytes = stream.peekBytes(SCAN_BLOCK_SIZE);
232232
var scanLength = scanBytes.length - ENDSTREAM_SIGNATURE_LENGTH;
233+
if (scanLength <= 0) break; // no match possible, end of stream or invalid stream
233234
var found = false, i, ii, j;
234235
for (i = 0, j = 0; i < scanLength; i++) {
235236
var b = scanBytes[i];
@@ -253,7 +254,7 @@ var Parser = (function ParserClosure() {
253254
stream.pos += scanLength;
254255
}
255256
if (!found) {
256-
error('Missing endstream');
257+
error('Missing endstream or invalid stream');
257258
}
258259
length = skipped;
259260

base/display/canvas.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -902,8 +902,10 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
902902
this.current.font = fontObj;
903903
this.current.fontSize = size;
904904

905-
if (fontObj.coded)
906-
return; // we don't need ctx.font for Type3 fonts
905+
if (fontObj.coded) {
906+
warn('Unsupported Type3 font (custom Glyph) - ' + fontRefName);
907+
return; // we don't need ctx.font for Type3 fonts
908+
}
907909

908910
var name = fontObj.loadedName || 'sans-serif';
909911
var bold = fontObj.black ? (fontObj.bold ? 'bolder' : 'bold') :
@@ -1627,7 +1629,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
16271629
//MQZ. make sure endGroup is always invoked after beginGroup
16281630
if (this.groupLevel == 0)
16291631
this.beginGroup(group);
1630-
1632+
16311633
this.groupLevel--;
16321634
var groupCtx = this.ctx;
16331635
this.ctx = this.groupStack.pop();

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"test:jest": "jest --config ./jest.config.json",
3838
"test": "jest --config ./jest.config.json && npm run parse-r && npm run parse-fd",
3939
"test:forms": "cd ./test && sh p2j.forms.sh",
40-
"test:misc": "cd ./test && sh p2j.one.sh misc . \"Expected: 13 success, 5 fail exception with stack trace\" ",
40+
"test:misc": "cd ./test && sh p2j.one.sh misc . \"Expected: 13 success, 6 exceptions with stack trace\" ",
4141
"parse": "./bin/pdf2json.js -f ./test/pdf/fd/form/F1040.pdf -o ./test/target/fd/form",
4242
"parse-s": "./bin/pdf2json.js -f ./test/pdf/fd/form/F1040.pdf -o ./test/target/fd/form -s",
4343
"parse-t": "./bin/pdf2json.js -f ./test/pdf/fd/form/F1040.pdf -o ./test/target/fd/form -s -t",
@@ -48,7 +48,7 @@
4848
"parse-tb": "./bin/pdf2json.js -f ./test/pdf/misc/i242_testingWithTable.pdf -o ./test/target/misc",
4949
"parse-tc": "./bin/pdf2json.js -f ./test/pdf/misc/i293_pdfpac.pdf -o ./test/target/misc",
5050
"parse-rectFix": "./bin/pdf2json.js -f ./test/pdf/misc/pr298_rect_fix_from_upstream.pdf -o ./test/target/misc",
51-
"parse-e": "./bin/pdf2json.js -f ./test/pdf/misc/i373_err_broken.pdf -o ./test/target/misc",
51+
"parse-e": "./bin/pdf2json.js -f ./test/pdf/misc/i306_err_invalid.pdf -o ./test/target/misc",
5252
"build:rollup": "rollup -c ./rollup.config.js",
5353
"build:bundle-pdfjs-base": "node rollup/bundle-pdfjs-base.js",
5454
"build": "npm run build:bundle-pdfjs-base && npm run build:rollup",

test/pdf/misc/i306_err_invalid.pdf

120 KB
Binary file not shown.

0 commit comments

Comments
 (0)