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).
This commit is contained in:
aiongg committed 2026-10-07 19:57:10 +00:00
1 parent 206d1c157d
commit ece97ec5f1
7 files changed
+133 -39

No files matched your search

+40 -18
View File
@@ -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<Spread>, Vec<Spread>), Error> {
let mut spreads = self
.uid_list(DOC, chunk::DOC_SPREADS)?
.into_iter()
.map(|uid| self.spread(uid))
.collect::<Result<Vec<_>, _>>()?;
let mut master_spreads = self
.uid_list(DOC, chunk::DOC_MASTER_SPREADS)?
.into_iter()
.map(|uid| self.spread(uid))
.collect::<Result<Vec<_>, _>>()?;
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<Vec<Story>, 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<T>(
&self,
what: &str,
list: u32,
read: impl Fn(u32) -> Result<Option<T>, Error>,
) -> Result<Vec<T>, 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,
}
}
+14 -7
View File
@@ -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)? {
+27
View File
@@ -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]| {
+8 -2
View File
@@ -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,
+16
View File
@@ -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<Attrs, Error>,
) -> Option<Attrs> {
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);
+6 -3
View File
@@ -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 {
+22 -9
View File
@@ -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