PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
fix(xfdf): use an imported redaction mark's /Rect only when it is four finite numbers
aiongg256097023c57
diff --git a/crates/xfdf/src/lib.rs b/crates/xfdf/src/lib.rsindex 9582298b3..219b0de81 100644--- a/crates/xfdf/src/lib.rs+++ b/crates/xfdf/src/lib.rs@@ -246,6 +246,15 @@ fn nums_of(doc: &Document, o: Option<&Object>) -> Vec<f64> {o.map(|o| doc.resolve(o)).and_then(|a| a.as_array().map(|a| a.iter().filter_map(|x| doc.resolve(x).as_f64()).collect())).unwrap_or_default()}+/// A `/Rect` of exactly four finite numbers; any other entry makes the whole rectangle unusable.+fn rect_strict(doc: &Document, o: Option<&Object>) -> Option<[f64; 4]> {+ let num = |x: &Object| doc.resolve(x).as_f64().filter(|v| v.is_finite());+ match doc.resolve(o?).as_array()?.as_slice() {+ [x0, y0, x1, y1] => Some([num(x0)?, num(y0)?, num(x1)?, num(y1)?]),+ _ => None,+ }+}+/// Whether a quad's four corners enclose any area: some three of them are not collinear.fn quad_has_area(q: &[f64; 8]) -> bool {let [ax, ay, bx, by, cx, cy, dx, dy] = *q;@@ -1053,8 +1062,8 @@ fn place(doc: &mut Document, page_ref: ObjRef, mut d: Dict) -> Result<ObjRef, Da// Quads the outline can't draw, or any quad that encloses no area, are replaced by the// /Rect's, or dropped when there is no usable /Rect either; such a mark then removes// nothing.- match nums_of(doc, d.get(b"Rect"))[..] {- [x0, y0, x1, y1] if [x0, y0, x1, y1].iter().all(|v| v.is_finite()) => {+ match rect_strict(doc, d.get(b"Rect")) {+ Some([x0, y0, x1, y1]) => {let (l, r, b, t) = (x0.min(x1), x0.max(x1), y0.min(y1), y0.max(y1));d.set(b"QuadPoints".to_vec(), arr(&[l, t, r, t, l, b, r, b]));}diff --git a/crates/xfdf/src/tests.rs b/crates/xfdf/src/tests.rsindex de880fe66..c98fab06a 100644--- a/crates/xfdf/src/tests.rs+++ b/crates/xfdf/src/tests.rs@@ -1751,3 +1751,10 @@ fn an_imported_redaction_mark_with_zero_area_quads_falls_back_to_its_rect() {assert_eq!(quads, rect_quads, "zero-area quads were kept: {mark:?}");}}++#[test]+fn an_imported_redaction_rect_with_a_non_number_is_not_used() {+ let fdf = b"%FDF-1.2\n1 0 obj\n<< /FDF << /Annots [<< /Subtype /Redact /NM (r1) /Page 0 /Rect [0 0 /Bad 100 100] >>] >> >>\nendobj\ntrailer\n<< /Root 1 0 R >>\n%%EOF\n";+ let mark = imported_redact_mark(fdf);+ assert!(mark.get(b"QuadPoints").is_none(), "quads were drawn from a malformed /Rect: {mark:?}");+}