craft-xlsx: review fixes: array constants bounded per formula, a spent allowance skips decoding

- A formula's array constants share one allowance of 65,536 values, each
  array charged before its rows are allocated. Before, the cap was per
  array, so a formula of many arrays could hold gigabytes before the formula
  text allowance was checked.
- Once binary_formula_text is spent, a formula is not decoded only to be
  dropped: it keeps its saved result straight away.
This commit is contained in:
kiwi4boy committed 2026-11-14 12:35:54 -05:00
1 parent 5742279729
commit fa5457bed6
4 files changed
+45 -7

No files matched your search

+3 -2
View File
@@ -104,8 +104,9 @@ other workbooks, Python code stored outside the cell, data tables. The XLSB func
(40 million records, 4 million cells and as many shared strings, 256 MiB of text, as
`craft-xls` allows). `binary_formula_text`: decoded formula text over the workbook (128 MiB);
past it, formulas keep their saved results. A formula's tokens are bounded at 16 KB, its
nesting at 256 levels and an array constant at 65,536 values. Fixed caps: 1024 sheets, 100,000 merged ranges per sheet, 64 nested
extension blocks.
nesting at 256 levels and its array constants at 65,536 values in all, each charged before
it is held; once the formula text allowance is spent, no formula is decoded. Fixed caps: 1024
sheets, 100,000 merged ranges per sheet, 64 nested extension blocks.
- Cells and rows outside the 1,048,576 × 16,384 grid are dropped and counted.
SpreadsheetML is read leniently: a damaged optional part is a warning, a damaged worksheet keeps
+5
View File
@@ -523,6 +523,11 @@ fn decode_formula(
warnings: &mut Warnings,
) -> Option<String> {
let links = links?;
// Once the allowance is spent no formula fits it, so none is decoded only to be dropped.
if !budget.formula_text_fits(1, limits) {
kept(warnings, "more formula text than the limits allow");
return None;
}
let text = match formula::decode(rgce, rgcb, links, mode) {
Ok(e) => expr::print(&e),
Err(formula::Unsupported(why)) => {
+10 -5
View File
@@ -18,7 +18,8 @@ use super::ftab::function;
/// Formula token bytes decoded at most (a cell formula holds at most 16,384).
pub(super) const MAX_RGCE: usize = 16 * 1024;
/// Values in one array constant at most.
/// Values in one formula's array constants at most, all of them together: each array is charged
/// before its values are held, so a formula of many arrays can't hold more than this.
const MAX_ARRAY: usize = 65_536;
/// Why a formula could not be turned back into text.
@@ -301,6 +302,7 @@ pub(super) fn decode(rgce: &[u8], rgcb: &[u8], links: &Links, mode: Mode) -> Res
let mut t = Bytes { b: rgce, pos: 0 };
let mut extra = Bytes { b: rgcb, pos: 0 };
let mut stack: Stack = Vec::new();
let mut array_values = MAX_ARRAY;
let mut ws = Spacing::default();
while t.pos < rgce.len() {
let ptg = t.u8()?;
@@ -414,7 +416,7 @@ pub(super) fn decode(rgce: &[u8], rgcb: &[u8], links: &Links, mode: Mode) -> Res
0x20 => {
// PtgArray: 14 unused bytes (Excel leaves junk in them); the values are in rgcb.
t.take(14)?;
push(&mut stack, kids, array_constant(&mut extra)?)?;
push(&mut stack, kids, array_constant(&mut extra, &mut array_values)?)?;
}
0x21 => {
let iftab = t.u16()?;
@@ -663,12 +665,15 @@ fn grouped(e: Expr, in_args: bool) -> Expr {
/// PtgExtraArray ([MS-XLSB] 2.5.98.41): row and column counts, then `SerAr` values in row-major
/// order. The type codes are not BIFF8's: 0x00 is a number here, not a missing value.
fn array_constant(b: &mut Bytes<'_>) -> Result<Expr, Unsupported> {
fn array_constant(b: &mut Bytes<'_>, left: &mut usize) -> Result<Expr, Unsupported> {
let rows = usize::try_from(b.u32()?).map_err(|_| TRUNC)?;
let cols = usize::try_from(b.u32()?).map_err(|_| TRUNC)?;
if rows == 0 || cols == 0 || rows.saturating_mul(cols) > MAX_ARRAY {
return Err(Unsupported("array constant too large or empty"));
let values = rows.saturating_mul(cols);
if values == 0 {
return Err(Unsupported("empty array constant"));
}
// Charged before the rows are allocated, against what the formula's arrays may hold in all.
*left = left.checked_sub(values).ok_or(Unsupported("array constants too large"))?;
let mut out = Vec::with_capacity(rows);
for _ in 0..rows {
let mut row = Vec::with_capacity(cols);
+27
View File
@@ -1035,3 +1035,30 @@ fn references_to_macro_sheets_and_spans_over_chart_sheets_are_named() {
assert_eq!(got, [Some("Macro1!$A$1"), Some("Data:Hidden!$A$1"), None]);
assert!(warned(&wb, "references to sheets with no cells"), "{:?}", wb.warnings);
}
#[test]
fn a_formulas_array_constants_share_one_allowance() {
// Two arrays of 40,000 Boolean values, each within what one array may hold, added together:
// charged before their values are held, together they pass what a formula's arrays may hold
// (65,536 values), so the cell keeps its saved result. One of them alone decodes.
let array = |n: u32| [&n.to_le_bytes()[..], &1u32.to_le_bytes(), &[2u8, 1].repeat(n as usize)].concat();
let ptg = [&[0x60u8][..], &[0; 14]].concat();
let two = [&ptg[..], &ptg, &[0x03]].concat();
let data = [row(0), formula_cell(0, &two, &[array(40_000), array(40_000)].concat()), formula_cell(1, &ptg, &array(40_000))].concat();
let wb = read(&package(&book_with_links(&xti()), &worksheet(&data, &[]), &sst(), ROOT, RELS, TYPES)).unwrap();
assert_eq!(wb.sheets[0].formula(CellRef::new(0, 0)), None);
assert_eq!(wb.sheets[0].value(CellRef::new(0, 0)), Value::Number(0.0));
assert!(wb.sheets[0].formula(CellRef::new(0, 1)).is_some_and(|f| f.starts_with("{TRUE;TRUE")));
assert!(warned(&wb, "array constants too large"), "{:?}", wb.warnings);
}
#[test]
fn a_spent_formula_allowance_skips_decoding() {
// With no allowance, a formula is not decoded at all: an unknown token is never reached.
let data = [row(0), formula_cell(0, &[0xFF], &[])].concat();
let limits = Limits { binary_formula_text: 0, ..Limits::default() };
let wb = read_with(&package(&book_with_links(&xti()), &worksheet(&data, &[]), &sst(), ROOT, RELS, TYPES), &limits).unwrap();
assert_eq!(wb.sheets[0].formula(CellRef::new(0, 0)), None);
assert!(warned(&wb, "more formula text than the limits allow"), "{:?}", wb.warnings);
assert!(!warned(&wb, "unknown formula token"), "{:?}", wb.warnings);
}