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.
This commit is contained in:
dotCooCoo committed 2026-11-27 21:47:49 -08:00
1 parent 7b0a60be9b
commit f23186d1d4
3 files changed
+32 -6

No files matched your search

+6 -3
View File
@@ -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);
}
+3 -3
View File
@@ -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()
}
}
+23
View File
@@ -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]