mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 15:55:14 +00:00
craft-drawingml: tighten ListStyle size guard, add deep group stack test
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2632c41eb4
commit
d5b4dbef88
2 files changed
+37
-8
No files matched your search
@@ -766,11 +766,11 @@ mod tests {
|
||||
use crate::color::Rgba;
|
||||
|
||||
/// `ListStyle` sits inline in every `TextBody`/`Shape`, which the pptx group reader holds on the
|
||||
/// stack once per nesting level. Keep it small (the `defPPr` level is boxed) so a debug build
|
||||
/// does not overflow the 1 MiB Windows main-thread stack.
|
||||
/// stack once per nesting level. Keep it at `Vec` (24) + `Option<Box>` (8) bytes (the `defPPr`
|
||||
/// level is boxed) so a debug build does not overflow the 1 MiB Windows main-thread stack.
|
||||
#[test]
|
||||
fn list_style_stays_small() {
|
||||
assert!(std::mem::size_of::<ListStyle>() <= 64, "{}", std::mem::size_of::<ListStyle>());
|
||||
assert!(std::mem::size_of::<ListStyle>() <= 32, "{}", std::mem::size_of::<ListStyle>());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -217,23 +217,52 @@ fn other_files_are_refused() {
|
||||
assert!(craft_pptx::import_theme(&doc).is_err());
|
||||
}
|
||||
|
||||
/// Groups nested past 32 levels are cut there; the rest of the slide is read.
|
||||
#[test]
|
||||
fn deep_group_nesting_is_cut() {
|
||||
/// A slide whose group tree is 40 levels deep, with a shape after it.
|
||||
fn deep_group_deck() -> Vec<u8> {
|
||||
let open = r#"<p:grpSp><p:nvGrpSpPr><p:cNvPr id="0" name="G"/><p:cNvGrpSpPr/><p:nvPr/></p:nvGrpSpPr><p:grpSpPr/>"#;
|
||||
let tree = format!("{}{}{}", open.repeat(40), sp(4, "Deep", "", XFRM, "x"), "</p:grpSp>".repeat(40));
|
||||
let pres = format!(r#"<p:presentation {NS}><p:sldIdLst><p:sldId id="256" r:id="rId1"/></p:sldIdLst></p:presentation>"#);
|
||||
let slide = format!(r#"<p:sld {NS}><p:cSld><p:spTree>{tree}{}</p:spTree></p:cSld></p:sld>"#, sp(5, "Top", "", XFRM, "y"));
|
||||
let bytes = package(
|
||||
package(
|
||||
&[
|
||||
("ppt/presentation.xml", CT_PRES, pres),
|
||||
("ppt/slides/slide1.xml", "application/vnd.openxmlformats-officedocument.presentationml.slide+xml", slide),
|
||||
],
|
||||
&[("", &[("officeDocument", "ppt/presentation.xml")]), ("ppt/presentation.xml", &[("slide", "slides/slide1.xml")])],
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
/// Groups nested past 32 levels are cut there; the rest of the slide is read.
|
||||
#[test]
|
||||
fn deep_group_nesting_is_cut() {
|
||||
let bytes = deep_group_deck();
|
||||
let p = craft_pptx::import(&bytes, &Plain).unwrap();
|
||||
let mut depth = 0;
|
||||
craft_pptx::walk(&p.slides[0].shapes, &mut |_, d| depth = depth.max(d));
|
||||
assert_eq!(depth, 32, "33 levels of groups, the last one empty");
|
||||
assert!(p.slides[0].shapes.iter().any(|s| s.name == "Top"));
|
||||
}
|
||||
|
||||
/// The group reader recurses once per nesting level and keeps a `Shape` per level on the stack, so
|
||||
/// anything that makes `Shape` (or `TextBody`/`ListStyle` inside it) bigger shows up here first.
|
||||
/// A debug build is the worst case, so import the deepest accepted tree on a stack that is small
|
||||
/// but ample for the current size: measured in a Linux debug build, this import needs about
|
||||
/// 1.3 MiB, and about 2 MiB when `ListStyle` holds its `defPPr` level inline. Fails with a stack
|
||||
/// overflow (the process aborts) when a type on the recursive path grows by value. Linux only:
|
||||
/// frame sizes differ per platform, and the Windows CI runs `deep_group_nesting_is_cut` on the
|
||||
/// default stack, where that growth first showed up.
|
||||
#[test]
|
||||
#[cfg(target_os = "linux")]
|
||||
fn deep_group_import_fits_a_small_stack() {
|
||||
const STACK: usize = 1664 * 1024;
|
||||
std::thread::Builder::new()
|
||||
.stack_size(STACK)
|
||||
.spawn(|| {
|
||||
let bytes = deep_group_deck();
|
||||
let p = craft_pptx::import(&bytes, &Plain).unwrap();
|
||||
assert!(p.slides[0].shapes.iter().any(|s| s.name == "Top"));
|
||||
})
|
||||
.unwrap()
|
||||
.join()
|
||||
.unwrap();
|
||||
}
|
||||
Reference in new issue
Block a user