Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

fix(audit): charge a line's runs to the comparison bound

Gabriel Luís da Silva52d49c301eab
diff --git a/crates/audit/src/lib.rs b/crates/audit/src/lib.rs
index 46823a1fa..f2bcf6335 100644
--- a/crates/audit/src/lib.rs
+++ b/crates/audit/src/lib.rs
@@ -138,10 +138,12 @@ struct Budget {
}
impl Budget {
- /// Resets the comparison count for a page with these `lines`.
+ /// Resets the comparison count for a page with these `lines`. A line costs its word boxes
+ /// or its runs, whichever are more (at least one): a cover check walks the boxes, and the
+ /// covered text walks the runs, which can draw no words at all (`( ) Tj`).
fn start_page(&mut self, lines: &[TextLine]) {
self.comparisons = 0;
- self.page_cost = lines.iter().fold(0, |n, line| n.saturating_add(line.ink.len().max(1)));
+ self.page_cost = lines.iter().fold(0, |n, line| n.saturating_add(line.ink.len().max(line.runs().count()).max(1)));
}
/// The text `cover` hides in the `lines` that `hides` ([`covered_words`]), joined with single
diff --git a/crates/audit/src/tests.rs b/crates/audit/src/tests.rs
index 7908980f1..e5fb9ab68 100644
--- a/crates/audit/src/tests.rs
+++ b/crates/audit/src/tests.rs
@@ -704,6 +704,28 @@ fn cover_and_line_comparisons_are_bounded() {
assert!(crate::audit(&doc, limits).expect("within the budget").is_empty());
}
+/// A run that draws no word still costs a comparison: the covered text walks every run of a
+/// covered line, so a word followed by many `( ) Tj` must not be checked for free (review on
+/// #4198).
+#[test]
+fn runs_without_words_count_against_the_comparison_bound() {
+ let mut content = b"BT /F1 24 Tf 100 700 Td (SECRET) Tj".to_vec();
+ content.extend_from_slice(&b" ( ) Tj".repeat(50));
+ content.extend_from_slice(
+ b" ET
+0 g
+98 688 94 32 re f
+",
+ );
+ let doc = open(one_page(&content, ""));
+ let lines = pdfcraft_edit::glyph_text_lines(&doc, &pdfcraft_model::pages(&doc)[0]).expect("text");
+ assert_eq!(lines.iter().map(|l| (l.ink.len(), l.runs().count())).collect::<Vec<_>>(), [(1, 51)]);
+ let limits = crate::Limits { comparisons: 50, covered_text: usize::MAX };
+ assert!(matches!(crate::audit(&doc, limits), Err(crate::AuditError::TooManyComparisons)));
+ let limits = crate::Limits { comparisons: 51, covered_text: usize::MAX };
+ assert_eq!(crate::audit(&doc, limits).expect("within the budget").len(), 1);
+}
+
/// Covered text is document text: a document whose permissions withhold copying reports where
/// its covers are but not what they hide, unless it was opened with the owner password.
#[test]