Compare commits

...
Author SHA1 Message Date
Dylan M. Taylor d58afe6112 Merge branch 'main' into fix/sec-header-footer-expansion-cap 2026-11-03 17:35:01 -05:00
Dylan M. Taylor 8024a21722 engine: cap header and footer sections at 64 KiB when fields expand
The 10,000-character header spec was bounded, but each &A, &F or &Z
added the whole sheet name or path again, and a JSON workbook's sheet
name has no length limit. A field that would take a section past
64 KiB is now left out.
2026-11-03 16:10:20 +00:00
2 changed files with 24 additions and 7 deletions
+18 -7
View File
@@ -153,6 +153,17 @@ pub struct HfSection {
pub size: Option<f32>,
}
/// Most bytes one section's expanded text may hold. A code like `&A` adds the whole sheet name
/// (or file path) each time it appears, and a JSON workbook's names have no length limit.
const MAX_SECTION_BYTES: usize = 64 * 1024;
/// Appends `s` unless the section would pass [`MAX_SECTION_BYTES`]; what doesn't fit is left out.
fn push_field(text: &mut String, s: &str) {
if text.len().saturating_add(s.len()) <= MAX_SECTION_BYTES {
text.push_str(s);
}
}
/// Expands a header/footer string into its left, centre and right sections.
pub fn expand_header_footer(spec: &str, cx: &HfContext) -> [HfSection; 3] {
let mut out: [HfSection; 3] = Default::default();
@@ -187,14 +198,14 @@ pub fn expand_header_footer(spec: &str, cx: &HfContext) -> [HfSection; 3] {
i += 1 + digits.len();
}
}
sec.text.push_str(&n.to_string());
push_field(&mut sec.text, &n.to_string());
}
'N' | 'n' => sec.text.push_str(&cx.pages.to_string()),
'D' | 'd' => sec.text.push_str(&cx.date),
'T' | 't' => sec.text.push_str(&cx.time),
'A' | 'a' => sec.text.push_str(&cx.sheet),
'F' | 'f' => sec.text.push_str(&cx.file),
'Z' | 'z' => sec.text.push_str(&cx.path),
'N' | 'n' => push_field(&mut sec.text, &cx.pages.to_string()),
'D' | 'd' => push_field(&mut sec.text, &cx.date),
'T' | 't' => push_field(&mut sec.text, &cx.time),
'A' | 'a' => push_field(&mut sec.text, &cx.sheet),
'F' | 'f' => push_field(&mut sec.text, &cx.file),
'Z' | 'z' => push_field(&mut sec.text, &cx.path),
'&' => sec.text.push('&'),
'B' | 'b' => sec.bold = !sec.bold,
'I' | 'i' => sec.italic = !sec.italic,
+6
View File
@@ -414,6 +414,12 @@ fn pdf_contains_text_and_header_codes_expand() {
assert_eq!(r.text, "P4/9 9:00 AM");
assert_eq!(expand_header_footer("plain", &cx)[1].text, "plain");
assert_eq!(expand_header_footer("&", &cx)[1].text, "");
// A megabyte sheet name repeated by 5,000 `&A` codes stops at the section's 64 KiB.
let long = HfContext { sheet: "S".repeat(1024 * 1024), ..cx };
let [_, c, _] = expand_header_footer(&"&A".repeat(5_000), &long);
assert!(c.text.is_empty(), "the name alone is over the limit");
let short = HfContext { sheet: "S".repeat(1000), ..Default::default() };
assert_eq!(expand_header_footer(&"&A".repeat(5_000), &short)[1].text.len(), 65_000);
}
#[test]