Skip to content

Fix UB in PdfObject as_string, as_name, as_bytes#245

Merged
messense merged 2 commits into
mainfrom
fix/pdf-object-owned-accessors
Jun 19, 2026
Merged

Fix UB in PdfObject as_string, as_name, as_bytes#245
messense merged 2 commits into
mainfrom
fix/pdf-object-owned-accessors

Conversation

@messense

Copy link
Copy Markdown
Owner

These accessors returned borrows (&str / &[u8]) tied to &self, but MuPDF resolves indirect references first and returns a pointer into the document's shared xref entry, not into self. Freeing that entry via delete_object then leaves the reference dangling, and since indirect objects alias the xref the borrow checker cannot prevent it.

Return owned data instead: as_string() -> String, as_name() -> Vec<u8>, as_bytes() -> Vec<u8>. The returned copies outlive any mutation of the document, closing the soundness hole unconditionally with no new types, crates, or error variants.

Closes #207.

These accessors returned borrows (`&str` / `&[u8]`) tied to `&self`, but
MuPDF resolves indirect references first and returns a pointer into the
document's shared xref entry, not into `self`. Freeing that entry via
`delete_object` then leaves the reference dangling, and since indirect
objects alias the xref the borrow checker cannot prevent it.

Return owned data instead: `as_string() -> String`, `as_name() ->
Vec<u8>`, `as_bytes() -> Vec<u8>`. The returned copies outlive any
mutation of the document, closing the soundness hole unconditionally
with no new types, crates, or error variants.

Closes #207.
@messense messense force-pushed the fix/pdf-object-owned-accessors branch from c9c272b to 69a9f84 Compare June 19, 2026 01:11
@messense messense requested a review from Copilot June 19, 2026 02:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a soundness hole in the PDF object accessor APIs by changing PdfObject::{as_string, as_name, as_bytes} to return owned data (String / Vec<u8>) rather than borrows tied to &self, preventing dangling references when MuPDF’s resolved xref-backed storage is freed via delete_object.

Changes:

  • Change PdfObject::as_string() to return String, and as_name() / as_bytes() to return Vec<u8> to eliminate UB from xref aliasing.
  • Update internal call sites (page resources, destinations/links, document parsing, and tests) to use the new owned-return APIs.
  • Add regression tests reproducing issue #207 by deleting indirect objects and attempting to observe dangling reads.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/pdf/object.rs Changes accessor return types to owned data to close the dangling-borrow UB hole.
tests/test_issues.rs Adds regression tests for issue #207 (indirect resolve + delete leading to dangling borrows).
src/pdf/page.rs Updates resource-name extraction and matching logic for owned as_name() results.
src/pdf/links/extraction.rs Updates link/destination parsing to use owned as_string() / as_name() results.
src/pdf/document.rs Updates document parsing helpers that previously relied on borrowed as_string() results.
src/shape/finish.rs Updates test code converting PDF names into Rust String using the new owned API.
src/destination.rs Updates destination kind decoding to match on owned name bytes via .as_slice().

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/pdf/object.rs
Comment thread src/pdf/object.rs
Comment thread src/pdf/object.rs

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated no new comments.

@messense messense merged commit 3ee6802 into main Jun 19, 2026
13 checks passed
@messense messense deleted the fix/pdf-object-owned-accessors branch June 19, 2026 04:11
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.

UB with PdfObject as_string, as_name, as_bytes methods

3 participants