From ece97ec5f17c56a4ece7755db2ccd86e6c0d8504 Mon Sep 17 00:00:00 2001 From: aiongg <67543387+aiongg@users.noreply.github.com> Date: Wed, 7 Oct 2026 19:57:10 +0000 Subject: [PATCH] fix: leave out an unreadable story, spread or page item with a warning One story, spread or page item that cannot be read no longer fails the whole document; it is left out with a warning, like other objects. An attribute list that cannot be parsed gives a warning instead of being dropped silently. Corpus output is unchanged; on 400 mutated copies of a fixture, 28 conversions fail instead of 33 (the rest are damaged database structures). --- src/model/document.rs | 58 +++++++++++++++++++++++++++++-------------- src/model/item.rs | 21 ++++++++++------ src/model/mod.rs | 27 ++++++++++++++++++++ src/model/prefs.rs | 10 ++++++-- src/model/reader.rs | 16 ++++++++++++ src/model/story.rs | 9 ++++--- src/model/style.rs | 31 ++++++++++++++++------- 7 files changed, 133 insertions(+), 39 deletions(-) diff --git a/src/model/document.rs b/src/model/document.rs index f522506..754f4c4 100644 --- a/src/model/document.rs +++ b/src/model/document.rs @@ -237,29 +237,48 @@ impl<'a> Reader<'a> { } /// The spreads and master spreads of the document, with the page - /// layout of each page resolved. + /// layout of each page resolved. A spread that cannot be read is left + /// out with a warning. fn document_spreads(&self) -> Result<(Vec, Vec), Error> { - let mut spreads = self - .uid_list(DOC, chunk::DOC_SPREADS)? - .into_iter() - .map(|uid| self.spread(uid)) - .collect::, _>>()?; - let mut master_spreads = self - .uid_list(DOC, chunk::DOC_MASTER_SPREADS)? - .into_iter() - .map(|uid| self.spread(uid)) - .collect::, _>>()?; + let mut spreads = + self.each_or_warn("spread", chunk::DOC_SPREADS, |u| self.spread(u).map(Some))?; + let mut master_spreads = + self.each_or_warn("master spread", chunk::DOC_MASTER_SPREADS, |u| { + self.spread(u).map(Some) + })?; resolve_page_layout(&mut spreads, &mut master_spreads); Ok((spreads, master_spreads)) } - /// The stories the document lists. + /// The stories the document lists. A story that cannot be read is + /// left out with a warning. fn document_stories(&self) -> Result, Error> { - self.uid_list(DOC, chunk::DOC_STORIES)? - .into_iter() - .filter(|&uid| self.class(uid) == Some(class::STORY)) - .map(|uid| self.story(uid)) - .collect() + self.each_or_warn("story", chunk::DOC_STORIES, |uid| { + match self.class(uid) == Some(class::STORY) { + true => self.story(uid).map(Some), + false => Ok(None), + } + }) + } + + /// Read each object of the document's UID list `list` with `read`. + /// An object that cannot be read is left out with a warning that + /// calls it `what`. + fn each_or_warn( + &self, + what: &str, + list: u32, + read: impl Fn(u32) -> Result, Error>, + ) -> Result, Error> { + let mut out = Vec::new(); + for uid in self.uid_list(DOC, list)? { + match read(uid) { + Ok(Some(t)) => out.push(t), + Ok(None) => {} + Err(e) => self.warn(format!("{what} {uid} left out: {e}")), + } + } + Ok(out) } /// The backing story of the XML structure: the document's chunk @@ -269,7 +288,10 @@ impl<'a> Reader<'a> { Some(d) if d.len() >= 4 => { let s = self.cursor(&d).u32()?; match self.class(s) { - Some(class::STORY) => Some(self.story(s)?), + Some(class::STORY) => self + .story(s) + .map_err(|e| self.warn(format!("XML story {s} left out: {e}"))) + .ok(), _ => None, } } diff --git a/src/model/item.rs b/src/model/item.rs index f587095..57fe160 100644 --- a/src/model/item.rs +++ b/src/model/item.rs @@ -504,9 +504,9 @@ impl<'a> Reader<'a> { Ok(paths) } - /// A page item and the items it contains. An item that contains - /// itself, or one nested more than [`MAX_ITEM_DEPTH`] deep, is left - /// out with a warning. + /// A page item and the items it contains. An item that cannot be + /// read, contains itself or is nested more than [`MAX_ITEM_DEPTH`] + /// deep is left out with a warning. pub(super) fn page_item( &self, uid: u32, @@ -534,7 +534,11 @@ impl<'a> Reader<'a> { self.item_path.borrow_mut().push(uid); let item = self.page_item_contents(uid, cls, layer); self.item_path.borrow_mut().pop(); - item + // An item that cannot be read is left out, like other objects. + Ok(item.unwrap_or_else(|e| { + self.warn(format!("item {uid} left out: {e}")); + None + })) } pub(super) fn page_item_contents( @@ -575,9 +579,12 @@ impl<'a> Reader<'a> { } let paths = self.paths(uid)?; let attrs = match self.chunk(uid, chunk::ITEM_ATTRS)? { - Some(d) => { - Attrs::parse(self.enc(), &d, List::Item, self.db.recorder()).unwrap_or_default() - } + Some(d) => self + .attrs_or_warn( + || format!("item {uid}"), + Attrs::parse(self.enc(), &d, List::Item, self.db.recorder()), + ) + .unwrap_or_default(), None => Attrs::default(), }; let object_style = match self.chunk(uid, chunk::ITEM_OBJECT_STYLE)? { diff --git a/src/model/mod.rs b/src/model/mod.rs index 2e254c5..1fd0c6a 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -105,6 +105,33 @@ mod tests { assert_eq!(reader.warnings.borrow().len(), 1); } + #[test] + fn items_that_cannot_be_read_are_left_out() { + // Group 10 holds group 11, whose transform is cut short, and + // group 12, whose attribute list is. + // Six bytes: a transform needs 48; an attribute list of 5 + // records needs more. + let short = |uid, id| { + let chunks = synthetic::chunks(&[(id, vec![5, 0, 0, 0, 0, 0])]); + (uid, class::GROUP, chunks) + }; + let objects = [ + group(10, &[11, 12]), + short(11, chunk::ITEM_TRANSFORM), + short(12, chunk::ITEM_ATTRS), + ]; + let bytes = synthetic::image(&objects); + let db = synthetic::database(&bytes, &objects); + let reader = Reader::new(&db); + let item = reader.page_item(10, None).unwrap().unwrap(); + assert_eq!(item.children.len(), 1); + assert_eq!(item.children[0].uid, 12); + let warnings = reader.warnings.borrow(); + assert_eq!(warnings.len(), 2, "{warnings:?}"); + assert!(warnings[0].starts_with("item 11 left out")); + assert!(warnings[1].starts_with("item 12: attribute list left out")); + } + #[test] fn style_group_cycles_are_cut() { let g = |uid, root, children: &[u32]| { diff --git a/src/model/prefs.rs b/src/model/prefs.rs index ea6738f..c038057 100644 --- a/src/model/prefs.rs +++ b/src/model/prefs.rs @@ -589,7 +589,10 @@ impl Reader<'_> { Some(d) if d.len() >= 12 => { let n = self.cursor(&d[8..]).u32()? as usize; d.get(12 + 12 * n..).and_then(|rest| { - Attrs::parse(self.enc(), rest, List::Item, self.db.recorder()).ok() + self.attrs_or_warn( + || format!("page item defaults {u}"), + Attrs::parse(self.enc(), rest, List::Item, self.db.recorder()), + ) }) } _ => None, @@ -671,7 +674,10 @@ impl Reader<'_> { if let Some(d) = get(chunk::STYLE_ATTRS)?.filter(|d| d.len() >= 2) { let mut c = self.cursor(&d); let n = c.u16()? as usize; - text_defaults = Attrs::parse_text(&mut c, n, List::Style, self.db.recorder()).ok(); + text_defaults = self.attrs_or_warn( + || "text defaults".into(), + Attrs::parse_text(&mut c, n, List::Style, self.db.recorder()), + ); } Ok(Prefs { values, diff --git a/src/model/reader.rs b/src/model/reader.rs index 3ae2d06..b85fac1 100644 --- a/src/model/reader.rs +++ b/src/model/reader.rs @@ -60,6 +60,22 @@ impl<'a> Reader<'a> { self.db.encoding().cursor(data) } + /// The attribute list `r`, or `None` with a warning that names the + /// list's owner (`what`) if it cannot be parsed. + pub(super) fn attrs_or_warn( + &self, + what: impl FnOnce() -> String, + r: Result, + ) -> Option { + match r { + Ok(a) => Some(a), + Err(e) => { + self.warn(format!("{}: attribute list left out: {e}", what())); + None + } + } + } + /// Record a problem that does not stop the conversion. pub fn warn(&self, msg: String) { self.warnings.borrow_mut().push(msg); diff --git a/src/model/story.rs b/src/model/story.rs index 7ef5237..b3a2f02 100644 --- a/src/model/story.rs +++ b/src/model/story.rs @@ -237,9 +237,12 @@ impl<'a> Reader<'a> { strand::PARAGRAPH_STYLE | strand::CHARACTER_STYLE => { let style = rc.u32()?; let n = rc.u16()? as usize; - let attrs = - Attrs::parse_text(&mut rc, n, List::Text, self.db.recorder()) - .unwrap_or_default(); + let attrs = self + .attrs_or_warn( + || format!("story {uid}, text formatting"), + Attrs::parse_text(&mut rc, n, List::Text, self.db.recorder()), + ) + .unwrap_or_default(); let list = if kind == strand::PARAGRAPH_STYLE { &mut para } else { diff --git a/src/model/style.rs b/src/model/style.rs index eeb82aa..61f838b 100644 --- a/src/model/style.rs +++ b/src/model/style.rs @@ -165,7 +165,11 @@ impl<'a> Reader<'a> { Some(d) if d.len() >= 2 => { let mut c = self.cursor(&d); let n = c.u16()? as usize; - Attrs::parse_text(&mut c, n, List::Style, self.db.recorder()).unwrap_or_default() + self.attrs_or_warn( + || format!("style {uid}"), + Attrs::parse_text(&mut c, n, List::Style, self.db.recorder()), + ) + .unwrap_or_default() } _ => Attrs::default(), }; @@ -295,17 +299,26 @@ impl<'a> Reader<'a> { builtin, based_on: uid_or_none(based_on), fitting: match self.chunk(uid, chunk::OBJECT_STYLE_FITTING)? { - Some(d) => { - Attrs::parse_short(self.enc(), &d, List::ObjectStyleFitting, self.db.recorder()) - .unwrap_or_default() - } + Some(d) => self + .attrs_or_warn( + || format!("object style {uid}, frame fitting"), + Attrs::parse_short( + self.enc(), + &d, + List::ObjectStyleFitting, + self.db.recorder(), + ), + ) + .unwrap_or_default(), None => Attrs::default(), }, attrs: match self.chunk(uid, chunk::OBJECT_STYLE_ATTRS)? { - Some(d) => { - Attrs::parse_short(self.enc(), &d, List::ObjectStyle, self.db.recorder()) - .unwrap_or_default() - } + Some(d) => self + .attrs_or_warn( + || format!("object style {uid}"), + Attrs::parse_short(self.enc(), &d, List::ObjectStyle, self.db.recorder()), + ) + .unwrap_or_default(), None => Attrs::default(), }, // The layout is known for these sizes only