From 182d47f61014734a04e487010b340fd762f9cb4e Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:57:48 +0000 Subject: [PATCH] Keep container bytes in owned ZIP archive --- crates/format/src/container.rs | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/crates/format/src/container.rs b/crates/format/src/container.rs index 32133b2b0..5544f2e9c 100644 --- a/crates/format/src/container.rs +++ b/crates/format/src/container.rs @@ -174,12 +174,10 @@ impl Writer { } } -/// A container being read. Opening reads all bytes into memory (needed because the archive -/// borrows them), checks the directory (the entry count, unique and plain names, every part's +/// A container being read. Opening reads all bytes into memory and checks the directory +/// (the entry count, unique and plain names, every part's /// declared size), not the parts. pub struct Reader { - /// The raw archive bytes, kept alive so the archive can borrow them. - bytes: Vec, archive: craft_zip::Archive<'static>, blobs: BTreeMap>, /// The bytes of payloads resolved so far, repeats counted: bounded by the expanded budget. @@ -198,13 +196,8 @@ impl Reader { let mut bytes = Vec::with_capacity(length as usize); source.read_to_end(&mut bytes).map_err(invalid)?; - // Leak the Vec to get 'static bytes (needed because Archive<'a> borrows them). - // This is acceptable: Reader holds the bytes for its lifetime anyway, and - // ZIP archives are typically small-to-moderate size. - let leaked: &'static [u8] = Box::leak(bytes.into_boxed_slice()); - - let mut archive = craft_zip::Archive::open( - leaked, + let mut archive = craft_zip::Archive::open_owned( + bytes, craft_zip::Limits { max_entries: limits.max_entries as u64, max_total_bytes: (limits.max_expanded)() as u64 }, ) .map_err(invalid)?; @@ -221,7 +214,7 @@ impl Reader { } } - Ok(Self { bytes: leaked.to_vec(), archive, blobs: BTreeMap::new(), hydrated: 0, document: None, limits }) + Ok(Self { archive, blobs: BTreeMap::new(), hydrated: 0, document: None, limits }) } /// The file's own part, `document.json`, read once and kept: provisional caches and the