Fix UB in PdfObject as_string, as_name, as_bytes#245
Merged
Conversation
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.
c9c272b to
69a9f84
Compare
Contributor
There was a problem hiding this comment.
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 returnString, andas_name()/as_bytes()to returnVec<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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 intoself. Freeing that entry viadelete_objectthen 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.