diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index d46c2ea20..2fdde59be 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -773,6 +773,10 @@ pub(crate) fn worksheet( } continue; } + // No formula record: the cell stays a value, so its formula's cell metadata goes too. + if let Some(cell) = row.as_mut().and_then(|r| r.cells.last_mut()).filter(|c| c.at == at) { + cell.cell_metadata = None; + } kept(warnings, "a shared or array formula that is missing from the file"); } match id { diff --git a/crates/xlsx/src/xlsb/formula.rs b/crates/xlsx/src/xlsb/formula.rs index 53b02f8a1..327ffcc25 100644 --- a/crates/xlsx/src/xlsb/formula.rs +++ b/crates/xlsx/src/xlsb/formula.rs @@ -477,6 +477,15 @@ fn literal_text(e: &Expr) -> bool { } } +/// How many strings a [`literal_text`] chain joins, or `None` for anything else. +fn literal_texts(e: &Expr) -> Option { + match e { + Expr::Text(_) => Some(1), + Expr::Binary(BinOp::Concat, a, b) => literal_texts(a)?.checked_add(literal_texts(b)?), + _ => None, + } +} + /// How tightly an expression binds when printed, as the formula parser reads it: binary /// operators by [`BinOp::precedence`], prefix `+`/`-` at 60 and `%` at 70 (`-2^2` is `(-2)^2`); /// anything else is one operand. @@ -512,8 +521,13 @@ fn paren(e: Expr) -> Expr { fn grouped(e: Expr, in_args: bool) -> Expr { match e { // Strings joined with `&` are written as one flat chain, however their tokens nest: `&` - // is associative, and `PY("…"&"…")` is read as Python code only in that form. - e @ Expr::Binary(BinOp::Concat, ..) if literal_text(&e) => { + // is associative, and `PY("…"&"…")` is read as Python code only in that form. The chain + // nests one level per string, so it is made only up to `MAX_DEPTH` strings, the bound + // `push` keeps on the decoded tree (Excel's 8,192 characters hold at most about 33 + // full-length constants). A longer one prints as its tokens nest, with the parentheses + // that needs: a balanced tree of thousands of strings is only a few levels deep, and + // printing or dropping a chain of thousands would recurse past the stack. + e @ Expr::Binary(BinOp::Concat, ..) if literal_texts(&e).is_some_and(|n| n <= MAX_DEPTH) => { let mut texts = vec![]; flatten_texts(e, &mut texts); let mut texts = texts.into_iter(); @@ -575,3 +589,46 @@ fn array_constant(b: &mut Bytes<'_>) -> Result { } Ok(Expr::Array(out)) } + +#[cfg(test)] +mod tests { + use super::*; + + /// Postfix tokens for `n` empty strings joined by `&` as a balanced tree. + fn balanced_strings(n: usize, out: &mut Vec) { + if n == 1 { + out.extend([0x17, 0, 0]); + } else { + balanced_strings(n / 2, out); + balanced_strings(n - n / 2, out); + out.push(0x08); + } + } + + #[test] + fn a_long_chain_of_strings_stays_within_the_depth_bound() { + // 4,096 strings and 4,095 `&` fill 16,383 token bytes, 13 levels deep as written. Made + // into one flat chain they would be 4,096 levels deep, past what printing and dropping + // the tree may recurse; on a small stack that overflows. They print as written instead. + let mut rgce = vec![]; + balanced_strings(4096, &mut rgce); + assert!(rgce.len() <= MAX_RGCE); + let text = std::thread::Builder::new() + .stack_size(256 * 1024) + .spawn(move || { + let e = decode(&rgce, &[], &Links::default(), Mode::Cell).unwrap(); + let text = super::super::expr::print(&e); + drop(e); + text + }) + .unwrap() + .join() + .unwrap(); + assert_eq!(text.matches("\"\"").count(), 4096); + // A short chain, such as Python code split into constants, is still one flat chain. + let mut short = vec![]; + balanced_strings(4, &mut short); + let e = decode(&short, &[], &Links::default(), Mode::Cell).unwrap(); + assert_eq!(super::super::expr::print(&e), "\"\"&\"\"&\"\"&\"\""); + } +} diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index 518ca5214..ee9ac3afd 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -909,12 +909,24 @@ fn a_dynamic_array_kept_as_its_value_has_no_cell_metadata() { part(1, (0, 1)), range_formula(426, (0, 1, 1, 1), &[0xFF]), // C1: a value with cell metadata keeps it, as the file has it. - meta, + meta.clone(), cell(5, 2, &1.0f64.to_le_bytes()), + // D1: the first cell of a dynamic array with no BrtArrFmla after it, then a value, E1. + meta.clone(), + part(3, (0, 3)), + cell(5, 4, &1.0f64.to_le_bytes()), + // F1: the same as the last cell of its row. + meta, + part(5, (0, 5)), + row(1), + cell(5, 0, &1.0f64.to_le_bytes()), ] .concat(); let wb = read(&file(&worksheet(&data, &[]))).unwrap(); let sheet = &wb.sheets[0]; - assert_eq!((sheet.formula(at("A1")), sheet.formula(at("B1"))), (None, None)); + for a in ["A1", "B1", "D1", "F1"] { + assert_eq!(sheet.formula(at(a)), None, "{a}"); + } assert_eq!(sheet.cell_metadata.iter().collect::>(), [(&at("C1"), &1)]); + assert!(warned(&wb, "missing from the file"), "{:?}", wb.warnings); }