From f23186d1d4f7b283032f66fb6344a074fc51c33b Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 27 Nov 2026 21:47:49 -0800 Subject: [PATCH] PDF import: count function evaluations on an import that stops part way The work count recorded a PDF import's function evaluations only after the last page, so an import that stopped on a later page (a page whose scan or drawing fails) recorded none of the evaluations its earlier pages took. Each page now adds its evaluations right after it is drawn, before any error returns, and each evaluation is counted once. --- crates/pdf/src/import/entry.rs | 9 ++++++--- crates/pdf/src/import_shading.rs | 6 +++--- crates/pdf/src/tests/import_fidelity.rs | 23 +++++++++++++++++++++++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/crates/pdf/src/import/entry.rs b/crates/pdf/src/import/entry.rs index 5bf57ec0f5..fbc9e1aef5 100644 --- a/crates/pdf/src/import/entry.rs +++ b/crates/pdf/src/import/entry.rs @@ -391,7 +391,12 @@ fn read_opened( merge_order(&mut order, &mut known, &marked); } b.begin_page(scan); - craft::pdf::interpret_page(page, &mut ctx, &mut b).map_err(|e| PdfError::Parse(e.to_string()))?; + let drawn = craft::pdf::interpret_page(page, &mut ctx, &mut b); + // The page's shadings take their evaluations while it is drawn: they count even when the + // import stops on this page or a later one. + #[cfg(any(test, feature = "read-count"))] + note_work(bytes, b.function_budget.take_spent(), 0); + drawn.map_err(|e| PdfError::Parse(e.to_string()))?; if incomplete.load(Ordering::Relaxed) { return Err(crate::import_scan::incomplete_content()); } @@ -485,8 +490,6 @@ fn read_opened( } } } - #[cfg(any(test, feature = "read-count"))] - note_work(bytes, b.function_budget.spent(), 0); if only_text && !art_boxes { return Err(PdfError::PlaceholderOnly); } diff --git a/crates/pdf/src/import_shading.rs b/crates/pdf/src/import_shading.rs index 4fcf950b6c..544208ed0b 100644 --- a/crates/pdf/src/import_shading.rs +++ b/crates/pdf/src/import_shading.rs @@ -215,10 +215,10 @@ impl FunctionBudget { true } - /// The evaluations taken from it (tests). + /// The evaluations taken from it since the last call (tests). #[cfg(any(test, feature = "read-count"))] - pub(crate) fn spent(&self) -> usize { - self.taken.get() + pub(crate) fn take_spent(&self) -> usize { + self.taken.take() } } diff --git a/crates/pdf/src/tests/import_fidelity.rs b/crates/pdf/src/tests/import_fidelity.rs index 596e029e3a..0be8dce82f 100644 --- a/crates/pdf/src/tests/import_fidelity.rs +++ b/crates/pdf/src/tests/import_fidelity.rs @@ -957,6 +957,29 @@ fn function_based_shadings_share_one_budget_per_import() { assert!(r.warnings.iter().any(|w| w.contains("too many to read")), "{:?}", r.warnings); } +/// An import that stops on a later page still counts the evaluations the shadings of the pages +/// before it took, once each. +#[test] +fn an_import_that_stops_on_a_later_page_counts_its_function_evaluations() { + use std::sync::atomic::Ordering::Relaxed; + let _held = shadings(); + let resources = format!( + "/Pattern << /P0 << /PatternType 2 /Shading << /ShadingType 1 /ColorSpace /DeviceRGB /Domain [0 4 0 1] /Matrix [25 0 0 100 0 0] /Function {} 0 R >> >> >>", + first_extra(2) + ); + let function = stream("/FunctionType 4 /Domain [0 4 0 1] /Range [0 1 0 1 0 1]", STRIPES); + let shaded = PdfPage { resources, ..PdfPage::new(100.0, 100.0, "/Pattern cs /P0 scn 0 0 100 100 re f") }; + // The second page's content ends inside a string, so the import stops there. + let cut = PdfPage::new(100.0, 100.0, "0 0 5 5 re f (unterminated"); + let bytes = pdf_with(&[shaded, cut], &[&function], None); + crate::import_shading::EVALUATIONS.store(0, Relaxed); + let before = function_evaluations_of(&bytes); + assert!(import(&bytes).is_err()); + let used = crate::import_shading::EVALUATIONS.load(Relaxed); + assert!(used > 0, "the first page's shading is read"); + assert_eq!(function_evaluations_of(&bytes) - before, used); +} + /// A page drawing one small form over and over makes objects only up to the import's limit, then /// leaves the rest out with a warning, instead of taking memory for every drawing (#3569). #[test]