Skip to content

Report or skip page tree objects of the wrong type instead of crashing - #1818

Open
amalsaidov wants to merge 2 commits into
LibrePDF:masterfrom
amalsaidov:malformed-page-tree-objects
Open

amalsaidov wants to merge 2 commits into
LibrePDF:masterfrom
amalsaidov:malformed-page-tree-objects

Conversation

@amalsaidov

Copy link
Copy Markdown
Contributor

Description of the new Feature/Bugfix

PdfReader crashed with unchecked exceptions on page trees whose objects have the wrong type (found by mutating the test PDFs):

Input Before After
A /Kids entry that refers to a non-dictionary, e.g. 42 ClassCastException in PageRefs.iteratePages the kid is skipped; partial reads report InvalidPdfException("The page tree is invalid.")
Catalog /Pages refers directly to a page (no /Kids) IndexOutOfBoundsException: Index -1 (no inherited attributes on the stack) read as a document with one page
Catalog without a /Pages dictionary NullPointerException on rootPages (full and partial reads) InvalidPdfException("The document has no page root."), like the missing catalog check just above it

This follows the rule in AGENTS.md that parsing untrusted files must not throw unexpected runtime exceptions, and matches how the reader already handles missing kids and cycles.

Related Issue: none

Unit-Tests for the new Feature/Bugfix

  • Unit-Tests added to reproduce the bug

Four cases added to PdfReaderMalformedPageTreeTest; all four fail on master.

Compatibilities Issues

No signature changes. A document without a page root now fails in the PdfReader constructor with InvalidPdfException (an IOException) instead of a NullPointerException. New message key the.document.has.no.page.root in en.lng.

Your real name

Amal Saidov

Testing details

./mvnw -B install and ./mvnw checkstyle:check pass locally on Java 25.

PdfReader threw a ClassCastException for a page tree kid that is not a
dictionary, an IndexOutOfBoundsException when the root of the page tree
is a page, and a NullPointerException when the catalog has no /Pages
dictionary. A kid of the wrong type is now skipped (or reported as an
invalid page tree on partial reads), a root page is read as one page,
and a missing page root is reported with an InvalidPdfException.
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 6 complexity · 0 duplication

Metric Results
Complexity 6
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

sonarqubecloud Bot commented Dec 4, 2026

Copy link
Copy Markdown

This branch has not been deployed

No deployments
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.

1 participant