Compare commits

...
Author SHA1 Message Date
Dylan M. Taylor 729129a8d0 style: rustfmt 2026-11-03 16:20:37 +00:00
Dylan M. Taylor 6120396fb1 fix(formats): hold ODT text:s spaces to a document-wide budget 2026-11-03 11:44:49 +00:00
4 changed files with 38 additions and 3 deletions
+2 -1
View File
File diff suppressed because one or more lines are too long
+14 -1
View File
@@ -157,8 +157,15 @@ pub(super) struct Body<'a> {
del: Option<DelState>,
/// Insertions open in the body: (region id, revision).
active_ins: Vec<(String, RevMark)>,
/// Spaces `text:s` may still add ([`MAX_EXPANDED_SPACES`]).
spaces_left: usize,
}
/// Most spaces the document's `text:s` elements add in all: each may ask for 1,000 in 20
/// bytes, so without a total a content.xml within its size limit could add 50 times its size.
/// Past it, a `text:s` adds one space.
pub(super) const MAX_EXPANDED_SPACES: usize = 16 << 20;
impl<'a> Body<'a> {
pub(super) fn new(styles: &'a Styles, pages: &'a Pages, zip: &'a mut dyn FnMut(&str) -> Option<Vec<u8>>) -> Self {
let page = pages.first.clone().unwrap_or_default();
@@ -195,6 +202,7 @@ impl<'a> Body<'a> {
info_field: None,
del: None,
active_ins: Vec::new(),
spaces_left: MAX_EXPANDED_SPACES,
}
}
@@ -822,7 +830,12 @@ impl Body<'_> {
self.fmt.push(fmt);
}
"s" => {
let count = get(a, "text:c").and_then(|v| v.trim().parse::<usize>().ok()).unwrap_or(1).clamp(1, 1000);
let asked = get(a, "text:c").and_then(|v| v.trim().parse::<usize>().ok()).unwrap_or(1).clamp(1, 1000);
let count = asked.min(self.spaces_left).max(1);
if count < asked && self.spaces_left > 0 {
log::warn!("odt: the document's spaces pass {MAX_EXPANDED_SPACES}; each further text:s adds one");
}
self.spaces_left = self.spaces_left.saturating_sub(count);
self.push_text(&" ".repeat(count));
}
"tab" => self.push_text("\t"),
+21
View File
@@ -1135,3 +1135,24 @@ fn meta_creation_date_timezones_and_fallbacks() {
assert_eq!(m2.created, "2024-03-05T13:45:00Z");
assert_eq!(m2.author, "Solo Creator");
}
#[test]
fn repeated_spaces_stop_growing_past_the_document_budget() {
// 20,000 `text:s` of 1,000 spaces each (20 million from 600 kB) are held to the budget;
// each one past it still adds a space.
let n = 20_000;
let spaces = r#"<text:s text:c="1000"/>"#.repeat(n);
let content = format!(
r#"<office:document-content {NS_T}><office:body><office:text><text:p>a{spaces}b</text:p></office:text></office:body></office:document-content>"#
);
let doc = import(&package(&content, None)).unwrap();
let text = doc.plain_text(wordcraft_doc::StoryRef::Body);
let got = text.chars().filter(|c| *c == ' ').count();
let full = body::MAX_EXPANDED_SPACES / 1000;
assert_eq!(got, body::MAX_EXPANDED_SPACES + (n - full - 1));
// A few spaces are kept as asked.
let content = format!(
r#"<office:document-content {NS_T}><office:body><office:text><text:p>a<text:s text:c="3"/>b</text:p></office:text></office:body></office:document-content>"#
);
assert!(import(&package(&content, None)).unwrap().plain_text(wordcraft_doc::StoryRef::Body).contains("a b"));
}
+1 -1
View File
File diff suppressed because one or more lines are too long