Skip to content

Fix #1601: guard against corrupt object-stream references in xref streams - #1602

Open
andreasrosdalw wants to merge 1 commit into
LibrePDF:masterfrom
andreasrosdalw:fix-1601-indexreader-oob
Open

Fix #1601: guard against corrupt object-stream references in xref streams#1602
andreasrosdalw wants to merge 1 commit into
LibrePDF:masterfrom
andreasrosdalw:fix-1601-indexreader-oob

Conversation

@andreasrosdalw

@andreasrosdalw andreasrosdalw commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Fixes IndexOutOfBoundsException in PdfReader #1601PdfReader could throw a raw IndexOutOfBoundsException ("Index 57407 out of bounds for length 899") when parsing a PDF whose cross-reference stream has a type 2 (compressed object) entry pointing at an object-stream number outside the document's actual object range.
  • Root cause: in PdfReader.readDocObj(), the objStmMark processing loop calls xrefObj.get(n) with no bounds or type check, unlike getPdfObject(int) a few hundred lines above, which already guards idx < 0 || idx >= xrefObj.size(). A corrupt/malformed xref stream can set that object-stream number to an arbitrary value (e.g. via a truncated or hand-edited PDF), and the unguarded .get(n) throws instead of failing gracefully.
  • Fix: skip an objStmMark entry when its key is out of range or does not actually resolve to a PRStream, mirroring the existing tolerant behavior of getPdfObject(int). A single corrupt compressed-object reference no longer prevents the rest of an otherwise valid document from parsing.

…xref streams

A cross-reference stream's type 2 (compressed object) entry can reference
an object-stream number that does not correspond to any parsed object
(e.g. a corrupt/malformed xref stream where the reference is far outside
the actual object range). PdfReader.readDocObj() looked up that object
number via xrefObj.get(n) without any bounds or type check, so a
malformed PDF could crash the reader with a raw IndexOutOfBoundsException
(or ClassCastException) instead of a handled parse error.

Skip such entries the same way getPdfObject(int) already tolerates
out-of-range indices, so a single corrupt compressed-object reference no
longer prevents the rest of an otherwise valid document from parsing.
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 14 complexity · 0 duplication

Metric Results
Complexity 14
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IndexOutOfBoundsException in PdfReader

1 participant