Skip to content

EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust

pdf: a soft mask at an unsupported bit depth is ignored, not fatal

midasmatterhorn400379fd2059
diff --git a/crates/pdf/src/image.rs b/crates/pdf/src/image.rs
index 5e6c49a54..1b39f062b 100644
--- a/crates/pdf/src/image.rs
+++ b/crates/pdf/src/image.rs
@@ -181,6 +181,11 @@ fn mask_image(file: &File, o: &Obj, soft: bool) -> Option<(Vec<u8>, usize, usize
return Some((data.into_iter().step_by(jpeg_n.unwrap_or(1)).take(w * h).collect(), w, h));
}
let bpc = if soft { file.get_num(d, "BitsPerComponent").unwrap_or(8.0) as usize } else { 1 };
+ // Like `decode()`: only 1/2/4/8/16-bit depths are supported. Anything else (notably
+ // `bpc >= 32`, where `1u32 << bpc` below would overflow) means "no usable mask".
+ if !matches!(bpc, 1 | 2 | 4 | 8 | 16) {
+ return None;
+ }
let max = ((1u32 << bpc) - 1).max(1) as f64;
let dec = file.get(d, "Decode").map(|x| file.nums(x)).filter(|v| v.len() >= 2);
let v = unpack(&data, w, h, 1, bpc);
diff --git a/crates/pdf/src/tests_import.rs b/crates/pdf/src/tests_import.rs
index add447e3a..064b5614d 100644
--- a/crates/pdf/src/tests_import.rs
+++ b/crates/pdf/src/tests_import.rs
@@ -263,6 +263,31 @@ fn hostile_structures_return_errors_or_draw_nothing() {
assert!(!doc.skipped.is_empty());
}
+#[test]
+fn smask_with_absurd_bpc_is_ignored_not_fatal() {
+ // `/BitsPerComponent 32` in a soft mask: `1u32 << bpc` must neither panic (debug)
+ // nor mistranslate samples into alpha (release); the mask is treated as absent.
+ let bytes = page_pdf(
+ "q 2 0 0 2 0 0 cm /Im Do Q",
+ "/XObject << /Im 10 0 R >>",
+ vec![
+ (
+ 10,
+ "<< /Type /XObject /Subtype /Image /Width 2 /Height 2 /ColorSpace /DeviceGray /BitsPerComponent 8 /SMask 11 0 R >>".into(),
+ Some(vec![0x80; 4]),
+ ),
+ (
+ 11,
+ "<< /Type /XObject /Subtype /Image /Width 2 /Height 2 /ColorSpace /DeviceGray /BitsPerComponent 32 >>".into(),
+ Some(vec![0xFF; 16]),
+ ),
+ ],
+ );
+ let doc = parse(&bytes).unwrap();
+ let img = render(&doc);
+ assert!(coverage(&img, 0, 0, 200, 100) > 0.0, "image still draws, opaque");
+}
+
#[test]
fn truncated_and_corrupt_eps_never_panic() {
let eps = b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 100 100\n\