Skip to content
Open
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
2 changes: 2 additions & 0 deletions src/core/parser/PDFParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ class PDFParser extends PDFObjectParser {
const firstInt = this.parseRawInt();
this.skipWhitespaceAndComments();

if (!IsDigit[this.bytes.peek()]) return xref;

const secondInt = this.parseRawInt();
this.skipWhitespaceAndComments();

Expand Down
49 changes: 49 additions & 0 deletions tests/core/parser/PDFParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,4 +395,53 @@ describe(`PDFParser`, () => {
const object2 = context.lookup(PDFRef.of(2));
expect(object2).toBeInstanceOf(PDFString);
});

it(`handles malformed xref entries gracefully`, async () => {
// xref entry missing generation number (malformed but recoverable)
const input = `
%PDF-1.7
1 0 obj
(test)
endobj
xref
0 2
0000000000 65535 f
0000000009 n
trailer
<< /Root 1 0 R /Size 2 >>
startxref
49
%%EOF
`;
const parser = PDFParser.forBytesWithOptions(typedArrayFor(input));
const context = await parser.parseDocument();

expect(context.enumerateIndirectObjects().length).toBe(1);
const object = context.lookup(PDFRef.of(1));
expect(object).toBeInstanceOf(PDFString);
});

it(`handles xref entries with a non-numeric generation number`, async () => {
const input = `
%PDF-1.7
1 0 obj
(test)
endobj
xref
0 2
0000000000 65535 f
0000000009 garbage n
trailer
<< /Root 1 0 R /Size 2 >>
startxref
49
%%EOF
`;
const parser = PDFParser.forBytesWithOptions(typedArrayFor(input));
const context = await parser.parseDocument();

expect(context.enumerateIndirectObjects().length).toBe(1);
const object = context.lookup(PDFRef.of(1));
expect(object).toBeInstanceOf(PDFString);
});
});
Loading