Prevent poppler page request for too large page numbers

This commit is contained in:
Julian Mutter 2024-12-08 15:21:49 +01:00
parent 636aa14be3
commit 9d8713f622

View File

@ -51,25 +51,27 @@ impl PageCache {
}
}
pub fn cache_page(&mut self, page_number: PageNumber, height: i32) -> Option<CacheResponse> {
pub fn cache_page(&mut self, page_number: PageNumber, height: i32) -> Result<Option<CacheResponse>> {
debug!("Caching page {}", page_number);
if page_number.abs_diff(self.last_requested_page_number)
> self.max_num_stored_pages.div_ceil(2)
{
debug!("Page too far from reader, aborting caching call");
return None;
bail!("Page too far from reader, aborting caching call");
}
let begin_of_cashing = Instant::now();
if let Some(page) = self.pages.get(&page_number) {
if page.height() >= height {
debug!("Page already in cache");
return None;
return Ok(None);
}
}
if page_number >= self.document.n_pages() as usize {
bail!("Requested page {} has too high number and is not in the document", page_number);
}
let begin_of_cashing = Instant::now();
let mut response = None;
println!("Getting page form poppler: {}", page_number);
if let Some(page) = self.document.page(page_number as i32) {
let pages = vec![Rc::new(page)];
let texture = draw::draw_pages_to_texture(&pages, height, &self.color_mode);
@ -91,7 +93,7 @@ impl PageCache {
page_number,
begin_of_cashing.elapsed().as_millis()
);
response
Ok(response)
}
fn remove_most_distant_page(&mut self) -> anyhow::Result<()> {
@ -130,7 +132,7 @@ impl PageCache {
fn process_command(&mut self, command: CacheCommand) -> Result<Option<CacheResponse>> {
debug!("Processing command: {:?}...", command);
match command {
CacheCommand::Cache(command) => Ok(self.cache_page(command.page, command.height)),
CacheCommand::Cache(command) => Ok(self.cache_page(command.page, command.height)?),
CacheCommand::Retrieve(command) => match command {
RetrievePagesCommand::GetCurrentTwoPages { page_left_number } => {
let page_left = self.get_page_or_cache(page_left_number)?;