diff --git a/crates/format/src/build.rs b/crates/format/src/build.rs index e922d35d3..bbffa98ac 100644 --- a/crates/format/src/build.rs +++ b/crates/format/src/build.rs @@ -83,11 +83,18 @@ fn family_lists(family: &Family, root: &Path, out: &mut String, mods: &mut Strin /// Whether a change file declares its `step` function (`fn step(`, `pub fn step (` …) on a line of /// code: a mention in a comment or a note is not a declaration. fn declares_step(text: &str) -> bool { - without_comments(text).lines().map(str::trim_start).any(|line| { - let line = line.strip_prefix("pub(crate)").or_else(|| line.strip_prefix("pub")).unwrap_or(line).trim_start(); - line.strip_prefix("fn").is_some_and(|rest| { - rest.starts_with(char::is_whitespace) && rest.trim_start().strip_prefix("step").is_some_and(|rest| rest.trim_start().starts_with('(')) - }) + let code = without_comments(text); + let ident = |c: char| c.is_alphanumeric() || c == '_'; + // `fn`, whitespace, `step`, then `(` (or generics), across lines + code.match_indices("fn").any(|(at, _)| { + let before_ok = code[..at].chars().next_back().is_none_or(|c| !ident(c)); + let rest = &code[at + 2..]; + before_ok + && rest.starts_with(char::is_whitespace) + && rest + .trim_start() + .strip_prefix("step") + .is_some_and(|rest| matches!(rest.trim_start().chars().next(), Some('(' | '<')) && !rest.starts_with(ident)) }) } diff --git a/crates/format/src/build_tests.rs b/crates/format/src/build_tests.rs index b97fc9ca9..79d3cb385 100644 --- a/crates/format/src/build_tests.rs +++ b/crates/format/src/build_tests.rs @@ -91,6 +91,10 @@ fn a_step_is_a_declaration_not_a_mention() { assert!(!declares_step("const S: &str = \"\nfn step(v) \";\n"), "inside a string"); // a comment between the words is whitespace assert!(declares_step("pub fn/* migration */step(v: &mut Value) {}\n")); + // across lines + assert!(declares_step("//! A change with a step.\npub fn step\n (_v: &mut Value) -> Result {\n Ok(())\n}\n")); + assert!(declares_step("pub fn\nstep(v: &mut Value) {}\n")); + assert!(!declares_step("fn stepper(v: &mut Value) {}\nfn prefn step() {}\n"), "other names"); assert!(declares_step("const X: u8 = 1; // fn step() is below\nfn step(v: &mut Value) {}\n")); assert!(declares_step("//! a note\npub(crate) fn step(v: &mut Value) -> Result<(), E> { Ok(()) }\n")); assert!(declares_step("//! a note\n fn step(v: &mut Value) {}\n")); diff --git a/crates/format/src/container.rs b/crates/format/src/container.rs index b1e4c1f34..00377baf5 100644 --- a/crates/format/src/container.rs +++ b/crates/format/src/container.rs @@ -229,6 +229,8 @@ pub struct Reader { pub hydrated: usize, document: Option>, limits: Limits, + /// Every byte part reads have expanded to, whatever the budget was at the time. + read_total: u64, } impl Reader { @@ -280,7 +282,7 @@ impl Reader { } } - Ok(Self { archive, blobs: BTreeMap::new(), hydrated: 0, document: None, limits }) + Ok(Self { archive, blobs: BTreeMap::new(), hydrated: 0, document: None, limits, read_total: 0 }) } /// The file's own part, `document.json`, read once and kept: provisional caches and the @@ -315,15 +317,17 @@ impl Reader { } // the expanded budget is read each time: a raised (or lowered) one applies at once, the // bytes already read still counting against it - let used = self.archive.budget().used(); let budget = self.archive.budget_mut(); *budget = craft_zip::Budget::new(budget.what(), (self.limits.max_expanded)() as u64); - let _ = budget.charge(used.min(budget.limit())); + if budget.charge(self.read_total).is_err() { + return Err(invalid("package too large")); + } // Read within budget. craft-zip's read() handles decompression bombs via the limit. let bytes = self.archive.read(name, limit as u64).map_err(invalid)?.ok_or_else(|| invalid("part not found"))?; if bytes.len() as u64 != expected { return Err(invalid("part length mismatch")); } + self.read_total = self.read_total.saturating_add(expected); Ok(bytes) } diff --git a/crates/format/src/container_tests.rs b/crates/format/src/container_tests.rs index a5f8fd773..6f9c06bbf 100644 --- a/crates/format/src/container_tests.rs +++ b/crates/format/src/container_tests.rs @@ -238,4 +238,23 @@ fn a_raised_expanded_budget_applies_at_once() { assert!(reader.limited_part("other.big", 4096).is_err(), "256 read + 512 is past 600"); BUDGET.store(1024, std::sync::atomic::Ordering::Relaxed); assert!(reader.limited_part("other.big", 4096).is_ok(), "768 fits the raised 1024"); + // a lowered budget does not forget what was read: 768 + 256 is past 1000 after a dip to 100 + BUDGET.store(100, std::sync::atomic::Ordering::Relaxed); + assert!(reader.limited_part("document.json", 4096).is_err()); + BUDGET.store(1000, std::sync::atomic::Ordering::Relaxed); + assert!(reader.limited_part("document.json", 4096).is_err(), "1024 is past 1000"); + BUDGET.store(1024, std::sync::atomic::Ordering::Relaxed); + assert!(reader.limited_part("document.json", 4096).is_ok()); +} + +/// An entry whose local header can't be read is refused at open, not skipped. +#[test] +fn an_unreadable_entry_is_refused() { + let mut writer = Writer::new(LIMITS); + writer.part("document.json", b"{}", false).unwrap(); + writer.part("other.json", b"[]", false).unwrap(); + let mut bytes = writer.finish().unwrap(); + let second = bytes.windows(4).enumerate().filter(|(_, w)| *w == b"PK\x03\x04").map(|(i, _)| i).nth(1).unwrap(); + bytes[second + 2] = 0; + assert!(Reader::new(Cursor::new(bytes), LIMITS).is_err()); } diff --git a/crates/format/src/newer.rs b/crates/format/src/newer.rs index e09d4f418..0a3141606 100644 --- a/crates/format/src/newer.rs +++ b/crates/format/src/newer.rs @@ -369,15 +369,12 @@ fn anchor_of(element: &Value) -> Option { element.get("name").and_then(Value::as_str).map(|name| Anchor::Name(name.to_string())) } -/// The identity of `element` that `known`'s records still carry: its id, or else its name. An -/// element with an identity neither of which survived has none. +/// The identity of `element` that `known`'s records still carry: its id, or else its name. When +/// typed reading kept neither, there is none, and the record is matched by its place. fn surviving_anchor(element: &Value, known: &Positions) -> Option { let id = element.get("id").and_then(Value::as_u64).map(Anchor::Id); let name = element.get("name").and_then(Value::as_str).map(|n| Anchor::Name(n.to_string())); - match (id, name) { - (Some(id), Some(name)) => Some(if known.find(usize::MAX, &id).is_some() { id } else { name }), - (id, name) => id.or(name), - } + [id, name].into_iter().flatten().find(|anchor| known.find(usize::MAX, anchor).is_some()) } /// Where each id and name first appears in a list, so matching records is not a scan per record. diff --git a/crates/format/src/newer_tests.rs b/crates/format/src/newer_tests.rs index bfbff081b..ae228405b 100644 --- a/crates/format/src/newer_tests.rs +++ b/crates/format/src/newer_tests.rs @@ -205,3 +205,14 @@ fn kept_elements_check_each_other() { // serde_json drops deep values recursively; leak it rather than overflow the test thread std::mem::forget(said); } + +/// A record whose whole identity typed reading drops is matched by its place while the list keeps +/// its length, so an unchanged save keeps its fields. +#[test] +fn a_record_without_a_surviving_identity_is_matched_by_place() { + let mut said = NewerFile::new("App", &newer()); + said.compare(&json!({"items": [{"id": 7, "extra": 1}]}), &json!({"items": [{}]}), "placeholder", &[]); + let mut saved = json!({"items": [{}]}); + assert!(said.restore_into(&mut saved).is_empty()); + assert_eq!(saved, json!({"items": [{"id": 7, "extra": 1}]})); +}