Skip to content

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

Merge remote-tracking branch 'origin/main' into pr-1482

bflatastic3ec09f20d4ef
diff --cc crates/pdf/src/cff.rs
index eb68a6a70,79e683873..275b75aa4
--- a/crates/pdf/src/cff.rs
+++ b/crates/pdf/src/cff.rs
@@@ -1054,61 -1057,29 +1062,87 @@@ mod tests
assert_eq!(p.bounding_box(), kurbo::Rect::new(50.0, 0.0, 550.0, 500.0));
}
+ /// An `endchar` seac whose base and accent name its own glyph used to recurse until the
+ /// stack overflowed and aborted the app; the depth limit cuts it off like the Type 1
+ /// interpreter's.
+ #[test]
+ fn seac_cycles_are_cut_off_not_a_stack_overflow() {
+ let enc = |v: i32| -> Vec<u8> {
+ if (-107..=107).contains(&v) {
+ vec![(v + 139) as u8]
+ } else {
+ let mut o = vec![28];
+ o.extend_from_slice(&(v as i16).to_be_bytes());
+ o
+ }
+ };
+ // `w adx ady bchar achar endchar`: both components are 'A' (standard code 65), this glyph.
+ let mut cs = vec![];
+ for v in [500, 0, 0, 65, 65] {
+ cs.extend(enc(v));
+ }
+ cs.push(14);
+ let font = write_test_cff(&[(".notdef", vec![14]), ("A", cs)]);
+ let cff = Cff::parse(&font).unwrap();
+ let (p, _) = cff.outline(cff.gid_by_name("A").unwrap()).unwrap();
+ assert!(p.elements().is_empty());
+ }
++
+ /// A CFF whose top DICT points its Private DICT at offsets near `usize::MAX`: `size` and `off`
+ /// come from the file, and reading the Private DICT must not overflow `off + size`.
+ #[test]
+ fn huge_private_dict_offsets_do_not_overflow() {
+ fn idx(items: &[Vec<u8>]) -> Vec<u8> {
+ let mut out = (items.len() as u16).to_be_bytes().to_vec();
+ if items.is_empty() {
+ return out;
+ }
+ out.push(4);
+ let mut off = 1u32;
+ out.extend_from_slice(&off.to_be_bytes());
+ for it in items {
+ off += it.len() as u32;
+ out.extend_from_slice(&off.to_be_bytes());
+ }
+ for it in items {
+ out.extend_from_slice(it);
+ }
+ out
+ }
+ let int = |v: i32| {
+ let mut o = vec![29];
+ o.extend_from_slice(&v.to_be_bytes());
+ o
+ };
+ // The BCD real "1E300": a size and an offset that both saturate at `usize::MAX`.
+ let huge = vec![0x1E, 0x1B, 0x30, 0x0F];
+ let header = [1u8, 0, 4, 4];
+ let names = idx(&[b"Test".to_vec()]);
+ let strings = idx(&[]);
+ let gsubrs = idx(&[]);
+ let charset = vec![0u8];
+ let cs = idx(&[vec![14]]); // one `.notdef` charstring: endchar
+ let top_len = 5 + 1 + 5 + 1 + huge.len() * 2 + 1;
+ let top_index_len = 2 + 1 + 8 + top_len;
+ let base = header.len() + names.len() + top_index_len + strings.len() + gsubrs.len();
+ let cs_off = base + charset.len();
+ let mut top = vec![];
+ top.extend(int(0));
+ top.push(15); // charset: ISOAdobe
+ top.extend(int(cs_off as i32));
+ top.push(17); // CharStrings
+ top.extend(&huge);
+ top.extend(&huge);
+ top.push(18); // Private: [size, offset]
+ assert_eq!(top.len(), top_len);
+ let mut out = header.to_vec();
+ out.extend(names);
+ out.extend(idx(&[top]));
+ out.extend(strings);
+ out.extend(gsubrs);
+ out.extend(charset);
+ out.extend(cs);
+ let cff = Cff::parse(&out).expect("the damaged Private DICT is skipped, the font parses");
+ assert_eq!(cff.glyph_count(), 1);
+ }
}