address review: close a replaced soft mask before nesting the new one; skip patterns whose wrappers pass the depth limit

This commit is contained in:
Dylan M. Taylor
2026-11-10 22:31:37 +00:00
parent 22b6aed8e5
commit 6a73739069
3 changed files with 52 additions and 3 deletions
+5
View File
@@ -48,6 +48,11 @@ impl Builder {
self.stack.len()
}
/// `wrap` more groups fit under the open ones within [`MAX_DEPTH`].
pub fn has_room(&self, wrap: usize) -> bool {
self.base.saturating_add(self.stack.len()).saturating_add(wrap) <= MAX_DEPTH
}
pub fn skip(&mut self, what: &str) {
if !self.skipped.iter().any(|s| s == what) {
self.skipped.push(what.to_string());
+9 -3
View File
@@ -636,6 +636,10 @@ impl<'a> Interp<'a> {
let bc = f.get(&sm, "BC").map(|b| f.nums(b)).unwrap_or_default();
let gcs = f.get_dict(&gd, "Group").and_then(|gr| f.get(gr, "CS")).map(|c| color_space(f, c, Some(res))).unwrap_or(Cs::Rgb);
let backdrop = if bc.is_empty() { [0.0; 3] } else { gcs.to_rgb(&bc) };
// The mask it replaces closes first: its groups don't enclose the new one.
if let Some(d) = self.gs.mask_depth {
self.close_to(d);
}
let inner = self.b.nested(2);
let saved_b = std::mem::replace(&mut self.b, inner);
let ctm = self.gs.ctm;
@@ -649,9 +653,6 @@ impl<'a> Interp<'a> {
for s in sk {
self.b.skip(&s);
}
if let Some(d) = self.gs.mask_depth {
self.close_to(d);
}
let mut content = Group::new("Soft Mask");
content.children = nodes;
let mut grp = Group::new("Soft Mask Group");
@@ -752,6 +753,11 @@ impl<'a> Interp<'a> {
if depth > 8 {
return;
}
// The `Pattern` and `Tile` wrappers must fit under the open groups too.
if !self.b.has_room(2) {
self.b.skip("group nesting limit");
return;
}
// The cell, drawn once in pattern space.
// The cell ends up under the open groups and this pattern's `Pattern` and `Tile`.
let inner = self.b.nested(2);
+38
View File
@@ -687,6 +687,44 @@ fn a_pattern_cell_counts_the_groups_it_is_nested_in() {
assert!(doc.skipped.iter().any(|s| s == "group nesting limit"), "{:?}", doc.skipped);
}
/// A pattern whose `Pattern` and `Tile` wrappers would pass the depth limit is skipped, not built
/// past it.
#[test]
fn a_pattern_whose_wrappers_pass_the_depth_limit_is_skipped() {
let clips = "0 0 200 100 re W n ".repeat(255);
let bytes = page_pdf(
&format!("{clips} /Pattern cs /P1 scn 0 0 100 100 re f"),
"/Pattern << /P1 10 0 R >>",
vec![(
10,
"<< /Type /Pattern /PatternType 1 /PaintType 1 /TilingType 1 /BBox [0 0 20 20] /XStep 20 /YStep 20 /Resources << >> >>".into(),
Some(b"0 0 10 10 re f".to_vec()),
)],
);
let doc = parse(&bytes).unwrap();
assert!(doc.skipped.iter().any(|s| s == "group nesting limit"), "{:?}", doc.skipped);
}
/// A soft mask replacing another is nested under what is open once the old one has closed, so
/// groups inside the old mask don't count against the new mask's content.
#[test]
fn a_replaced_soft_mask_does_not_count_against_the_new_one() {
let form = |n: &str| {
(
n.parse().unwrap(),
"<< /Type /XObject /Subtype /Form /BBox [0 0 200 100] /Group << /S /Transparency /CS /DeviceGray >> >>".to_string(),
Some(format!("{} 1 g 0 0 100 100 re f", "0 0 200 100 re W n ".repeat(250)).into_bytes()),
)
};
let bytes = page_pdf(
&format!("/GS1 gs {} /GS2 gs 1 0 0 rg 0 0 200 100 re f", "0 0 200 100 re W n ".repeat(250)),
"/ExtGState << /GS1 << /SMask << /S /Luminosity /G 10 0 R >> >> /GS2 << /SMask << /S /Luminosity /G 11 0 R >> >> >>",
vec![form("10"), form("11")],
);
let doc = parse(&bytes).unwrap();
assert!(doc.skipped.iter().all(|s| s != "group nesting limit"), "{:?}", doc.skipped);
}
/// Build isolated interpreter objects without presenting a function or colour space as a
/// document catalog. Complete-document parsing is covered by the page and reader fixtures.
fn object_file(objs: Objs) -> object::File {