Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect reference cycles when collecting page resources #298

Merged
merged 1 commit into from
Aug 13, 2024
Merged
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
27 changes: 16 additions & 11 deletions src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{Error, Result, Stream};
use encoding_rs::UTF_16BE;
use log::info;
use std::cmp::max;
use std::collections::{BTreeMap, HashMap};
use std::collections::{BTreeMap, HashMap, HashSet};
use std::io::Write;
use std::str;

Expand Down Expand Up @@ -421,24 +421,29 @@ impl Document {

/// Get resources used by a page.
pub fn get_page_resources(&self, page_id: ObjectId) -> (Option<&Dictionary>, Vec<ObjectId>) {
fn collect_resources(page_node: &Dictionary, resource_ids: &mut Vec<ObjectId>, doc: &Document) {
if let Ok(resources_id) = page_node.get(b"Resources").and_then(Object::as_reference) {
resource_ids.push(resources_id);
fn collect_resources(
page_node: &Dictionary, resource_ids: &mut Vec<ObjectId>, doc: &Document,
already_seen: &mut HashSet<ObjectId>,
) -> Result<()> {
if let Ok(resource_id) = page_node.get(b"Resources").and_then(Object::as_reference) {
resource_ids.push(resource_id);
}
if let Ok(page_tree) = page_node
.get(b"Parent")
.and_then(Object::as_reference)
.and_then(|id| doc.get_dictionary(id))
{
collect_resources(page_tree, resource_ids, doc);
if let Ok(parent_id) = page_node.get(b"Parent").and_then(Object::as_reference) {
if already_seen.contains(&parent_id) {
return Err(Error::ReferenceCycle);
}
already_seen.insert(parent_id);
let parent_dict = doc.get_dictionary(parent_id)?;
collect_resources(parent_dict, resource_ids, doc, already_seen)?;
}
Ok(())
}

let mut resource_dict = None;
let mut resource_ids = Vec::new();
if let Ok(page) = self.get_dictionary(page_id) {
resource_dict = page.get(b"Resources").and_then(Object::as_dict).ok();
collect_resources(page, &mut resource_ids, self);
collect_resources(page, &mut resource_ids, self, &mut HashSet::new()).unwrap();
}
(resource_dict, resource_ids)
}
Expand Down
5 changes: 4 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ pub enum Error {
PageNumberNotFound(u32),
/// Invalid object while parsing at offset.
Parse { offset: usize },
/// Dereferencing object failed due to a reference cycle.
ReferenceCycle,
/// Dereferencing object reached the limit.
/// This might indicate a reference loop.
ReferenceLimit,
Expand Down Expand Up @@ -67,7 +69,8 @@ impl fmt::Display for Error {
Error::Offset(o) => write!(f, "Invalid file offset: {}", o),
Error::PageNumberNotFound(p) => write!(f, "Page number {} could not be found", p),
Error::Parse { offset, .. } => write!(f, "Invalid object at byte {}", offset),
Error::ReferenceLimit => write!(f, "Could not dereference an object; possible reference loop"),
Error::ReferenceCycle => write!(f, "Could not dereference an object; reference cycle detected"),
Error::ReferenceLimit => write!(f, "Could not dereference an object; possible reference cycle"),
Error::StringDecode => write!(f, "Could not decode string"),
Error::Syntax(msg) => write!(f, "Syntax error: {}", msg),
Error::Trailer => write!(f, "Invalid file trailer"),
Expand Down
Loading