mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 04:04:25 +00:00
craft-xlsx: review fixes: a long string chain stays within the depth bound; pending array metadata
From the second review round (FredApps): - Strings joined with `&` are printed as one flat chain, which nests one level per string. A balanced tree of 4,096 empty strings fits in 16,383 token bytes and is 13 levels deep, but as a chain it was 4,096 levels, and printing and dropping it recurse: on a 256 KiB stack it overflowed. The chain is now 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, so Python code split into constants is unaffected). A longer one prints as its tokens nest, with the parentheses that needs. GridCraft's tree walks a left-deep chain in a loop; the printer copied from craft-xls recurses, which is how the port lost the bound. - The first cell of a shared or array formula whose BrtShrFmla/BrtArrFmla never comes (another cell or the next row follows) loses its cell metadata too, as a formula kept as its value does. The new unit test overflowed its stack without this commit; the extended metadata test fails without it. The Excel-saved workbook still reads with all 33 formulas as in its .xlsx.
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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<usize> {
|
||||
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<Expr, Unsupported> {
|
||||
}
|
||||
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<u8>) {
|
||||
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), "\"\"&\"\"&\"\"&\"\"");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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::<Vec<_>>(), [(&at("C1"), &1)]);
|
||||
assert!(warned(&wb, "missing from the file"), "{:?}", wb.warnings);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user