Commit Graph
14 Commits
Author SHA1 Message Date
5a850f6a72 XLSX: per-child allowances for streamed shared strings and Python scripts; warnings for repeated and out-of-order cells (#2532)
* XLSX: per-child allowances for streamed shared strings and Python scripts; warnings for repeated and out-of-order cells

The streaming parser that reads the shared-string table and the Python
part is told which children each streamed element may hold, and how
many: a shared string one `phoneticPr` and its text (`t`, `r`, `rPh`)
without a count; a `pythonScript` one `code`. An unknown name is
admitted once, for at most MAX_UNLISTED_CHILD_NAMES names. A child past
its allowance is dropped with its subtree and counted.

Inside a streamed element the element cap no longer fails the part: the
element that would pass it is dropped and counted, and so is each
element holding it up to the streamed element's own child, which is lost
whole. A child lost at the cap gives back its place in its name's
allowance, so a later child of that name can still be read. Later
children that fit, and later strings and scripts, are still read (main
kept only what came before). When the cap was already reached before the
streamed element that holds a child being dropped began (the innermost
open one, so also one that begins inside another), nothing in it can be
kept, and the part fails as on main, at the first element that closes
inside it. A streamed element that holds no element (`<si/>`,
`<si></si>`, or one holding only whitespace) is still handed out
with the cap full, as on main: the cap bounds the elements the parser
holds, and it holds none. The streamed element is marked cut
only when the child it lost has a listed name, and the mark records
which listed names it lost (a bit per name in `El::cut`, read with
`El::lost`); an unknown element holds nothing the reader reads. An
unknown name whose child is dropped at the cap gives back its place
among the unknown names the element may hold.

A shared string that lost text (a `t` or a run) at the cap keeps the
children that fit, with a warning, and drops its phonetic runs, since
their offsets point into the text as the file held it, and its phonetic
properties, which would otherwise be filed under the shortened text and
take the place of an intact string's reading of that text. The warning
counts every phonetic run dropped from a string that lost text, damaged
runs and those of a string that counts as missing included, and the
strings whose phonetic properties went. One that lost only phonetic
parts (an `rPh` or its `phoneticPr`) keeps its text and its other runs.
A string that lost text and kept none (an empty run kept before the lost
text holds none) counts as a missing string, as on main: its cells are
empty, the existing warning about missing shared strings is given, and
the strings after it keep their indexes (the table holds an Option per
string). A Python script that lost its code at the cap and kept no code
after it (none, or an empty one) counts as missing code: a cell naming
it keeps its cached value without Python code, as main does for missing
code. Unknown children of a script that fit are left out, as on main,
and a warning now counts them, also when the part turns out damaged
after them, as the shared-string reader does.

GridCraft places each worksheet cell where its address says, as on main,
a later cell read again where an earlier one was; a picture cell keeps
its picture, and an empty cell leaves a Python spill value. A warning
now counts cells that repeat an address and rows whose properties
are read again over ones already read (the later ones kept); another
counts cells out of column order or in another row's element. A cell
of a row element's own row at a column
held already, by that element or by the elements for the same row right
before it, is a repeat, whatever the reader stored for it (a bitmap of
the columns read, cleared when a row element of another row number
begins, up to the highest word the elements before it touched).
Otherwise a repeat is seen at the furthest position read so far, or
before it where a cell, a cell picture or a spill value is stored, so a
cell that repeats one the reader stores nothing for (a blank cell
without a style, a value inside another formula's array) is not counted
as a repeat when an element for another row sits between the two copies,
or one of them sits in another row's element, unless it is at the
furthest position; it is counted as out of place when it sits in another
row's element or out of column order. A cell counted as a repeat is
not also counted there; any other cell is, when it names another row
than the row element it sits in, whatever its column, or when its column
is not to the right of the rightmost column among the cells read before
it in that element. Only cells not past the furthest one are looked up.

Cell text longer than MAX_CELL_CHARS (UTF-16 units) is kept whole, and a
warning per sheet counts the long texts read: shared and inline strings,
the cached text of a formula that doesn't parse, and spill values, but
not a picture's fallback, a value inside another formula's array, or a
parsing formula's cached result. The count is taken as each cell is
read, so a long text that a later cell replaces is still counted.
The length is decided from the byte length where that is enough (up to
the limit in bytes it is within it, past three times the limit it is
over) and otherwise by counting bytes, not by decoding characters.
MAX_CELL_CHARS joins MAX_ROWS and MAX_COLS in gridcraft_core::addr,
and MAX_TEXT_CHARS in gridcraft_core::value is now derived from it;
docs/file-format-parity.md records the change.

Tests: crates/xlsx/src/tests/limits.rs (allowances at and past the
limit, unknown names, the cap at its edge and inside a run, a string
that loses only an unknown element at the cap, a string that lost all
its text at the cap, or kept only an empty run, a string that lost text
and had phonetic runs, a string that lost all its text and had phonetic
runs, one that lost text and kept its phonetic properties, before and
after an intact string with the same text, through a save, one that lost
only a phonetic run or its phonetic properties, one with empty text that
lost only a phonetic run, a string at the cap and one past it, an empty
`t` lost at the cap taken as lost text by name, run formatting and an
unknown element inside a run left out without a warning, repeated
cells and rows, repeats of cells the reader stores nothing for, in one
row element, in elements for the same row in a row, and after another
row's element, every sequence of up to three cells over two columns and
two rows split across up to three row elements against a plain reading
of the rule, out-of-order cells left of the rightmost one, cells in
columns CV and XFD of two rows in a row, long text at the limit and one
past it, a shared string over the limit in bytes but not in UTF-16
units, 20,000 repeats of a cell using a long shared string),
richdata_tests.rs (picture cells with repeats, through a save),
the parser's tests in xml.rs (a string inside another that begins with
the cap full, strings holding no element handed out with the cap full,
one under it and three in a row, which listed children a string lost at
the cap, an unknown name dropped at the cap freeing its place) and
python_part.rs, and
crates/engine/src/tests/python_files.rs (long spilled text, repeats in a
spill, code dropped at the cap, an empty script with the cap full, a
second code taken when the first is dropped at the cap, an empty code
after one dropped at the cap, a second code crossing the cap after a
kept one, a script without code whose unknown element crosses the cap, a
second code, through a save, unknown children of scripts counted in one
warning, none, and in a part damaged after them).

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Preserve text boundaries when shared-string runs are dropped

---------

Co-authored-by: Claude Fable 5.1 <[email protected]>
Co-authored-by: FredApps <[email protected]>
2026-12-04 09:43:08 +01:00
FredApps 505e303d13 Merge main into fix/2160-validate-json-workbook 2026-12-02 07:05:12 +01:00
dotCooCoo dec0e1a6c0 Deps: update zip past GHSA-m665-v7pm-7grj
The workspace required zip 3, which resolved to zip 3.0.0 for
gridcraft-xlsx, gridcraft-cli and gridcraft-engine's tests. zip 3.0.0
is affected by GHSA-m665-v7pm-7grj (parser differential through dual
local and central headers; affected through 9.0.4, patched in 9.0.5).
The requirement is now 9.0.5, which resolves to the zip 9.1.0 that
craft-update and craft-zip already use, so one zip remains in the
lock file and arbitrary and derive_arbitrary drop out.

zip 9 returns entry names as a Result, decoding names not flagged
UTF-8 as CP437. The package checker in gridcraft-cli lists an entry
whose name does not decode under its raw bytes made lossy, so it is
still checked and reported. Tests unwrap the decoded names.
2026-11-20 17:33:46 -08:00
nihilist-narwhalandClaude Opus 5.5 5f5dda7249 JSON workbooks: loaded ranges are ordered and on the sheet, damaged arrays are dropped (#2160)
`RangeRef` and `Array` were deserialized as written. A range with its corners swapped wrapped
`height()`/`width()` (about 4.29e9 in release, which then drove loops; a panic in debug, outside
`Session::execute` once it reached render or recalc), and an array shorter than rows × cols was
indexed past its end.

Like Excel with a damaged file, the open goes on and the damage is repaired:

- `RangeRef` loads through `RangeRef::new` with both corners clamped to the grid, so every range
  in a file (merges, tables, rules, print areas, charts…) is fixed in one place, and a file that
  was valid loads unchanged.
- `Value::is_well_formed` checks every array in a value, nested ones too, without recursion.
  Opening a JSON workbook drops a cell value that fails it before anything reads the cells, with
  a warning naming the cell; opening recalculates, so a formula there gets its value back.
  Python results already go through `enforce_python_limits`, whose check now uses it, so a
  damaged array nested in a result of the right shape is dropped too. The only stored values a
  JSON workbook holds are cell values and Python results (spills and parsed formulas aren't
  saved).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-09 23:17:21 -06:00
prxnvt 57d7ea9bc3 Engine: a Python reference that grows or shrinks drops the cell's kept result
Deleting part of a range that xl() reads (row 2 of A1:A3) shortens the
reference instead of making it #REF!, and inserting rows inside the range
lengthens it; either way the data the code reads changed, but the cell kept
its result. rewrite_python_refs now treats a reference whose rows or columns
changed in number like a deleted one: the cell is new code (#BLOCKED!) with no
kept result. A reference that only moves keeps the result.

Test: deleting_the_data_python_code_reads_drops_its_kept_result now covers a
partial deletion and an insertion inside the range (fails without the fix).
2026-11-02 02:52:38 -05:00
prxnvt e8f2becf4a Engine: a deleted reference drops a Python cell's kept result; spills read past the scan budget stay values
A structural edit that deleted the data an xl() reference names rewrote the
reference to #REF! but kept the cell's cached result, so the cell went on
showing a result for data that is gone. rewrite_python_refs now makes such a
cell new code (#BLOCKED!, no kept result), and the recalculation removes its
spill. A rename or a move, after which the reference names the same data,
still keeps the result.

When the reader's range-scan budget ran out part way through a Python spill,
the rest of its cells were read as plain cells, and the result rebuilt on open
was then blocked by them (#SPILL!). On such a sheet every Python spill now
keeps its values as plain values, with no live spill.

Tests: deleting_the_data_python_code_reads_drops_its_kept_result and
python_spills_read_past_the_scan_budget_keep_their_values_as_plain_values;
both fail without the fix.
2026-11-02 02:27:28 -05:00
prxnvt ad6aca08bf XLSX: a Python spill is recorded at its anchor, and one too large keeps its values
A Python cell's spill range was recorded only when the file held a record for
one of its other cells, so a result whose other cells are blank, which a file
may leave out, opened with no spill and no rebuilt result. The range is now
recorded when the anchor is read, if a result can fill it.

A Python range larger than a result may be (more than 1,000,000 cells, but
within what the reader tracks) emptied the cells it covered, although it is
no spill and file-format-parity.md says such cells keep the values the file
shows. They are now kept as plain values; a formula's spilled values are still
dropped, since recalculation fills them again.

Tests: a_python_spill_whose_blank_cells_have_no_records_is_still_a_spill and
a_python_spill_range_larger_than_a_result_keeps_its_values_as_plain_values;
both fail without the fix.
2026-11-02 02:02:07 -05:00
prxnvt 0151f43af8 Engine: python.set keeps a cell's result only for the same code and return type
python.set kept the stored result whenever the code was unchanged, also when
the call switched the return type between an Excel value and a Python object.
The cell then showed a value and preview of the other type, and a save wrote
the new type with that old cached value. The result is now kept only when both
the code and the return type match; otherwise the cell is new code, which
shows #BLOCKED! until it runs, and its old spill goes with its readers
recalculated.
2026-11-01 15:11:59 -05:00
prxnvt ec7ee8d4ea Engine: a Python cell that keeps only its value keeps its whole cached result
When a file is opened, a Python cell falls back to keeping only its value in
four cases: its code is missing from the Python part or the part is damaged
(the xlsx reader), its code is too long for a cell, its kept result is
malformed, or its result is past the adoption budget. The first kept the
values the cell spilled as plain values; the others dropped them, so values
the file showed were lost. All of them now follow one rule, through
Sheet::settle_spill: the anchor and every spilled cell keep their cached
values as plain values, with no live spill range; a cell's own content and
format are never overwritten; and the work is bounded by the spilled values
stored, not by the range. The values move from the spill into the cells, so
nothing is copied. The cut-by-budget test is renamed for what it now checks,
the dropped-result test now expects the values kept, and the adoption warning
and the file-format docs say so.
2026-11-01 13:28:19 -05:00
prxnvt 84d4f6aa84 XLSX: a Python script's length is measured after its escapes are decoded
A script's code was cut at the character limit before its ST_Xstring escapes
were decoded, so a script whose escapes (`_x0009_`, `_x000D_`) made the file's
text longer than the limit lost its end without a warning, although its code
fits. The code is now decoded first and then cut one character past the limit,
so a script that is too long once decoded still fails the check and its cell
keeps only its value, as before. Decoding is linear and reads at most a bounded
number of encoded characters, enough for any code that fits.
2026-11-01 12:58:13 -05:00
prxnvt e82e91b451 XLSX: a Python cell without its code keeps its whole cached result as values
A PY formula whose script the file doesn't hold (the Python part missing or
damaged, or the script over the code budget) keeps only its cached value, but
its array range stayed recorded, so the cached values over the rest of its
spill were dropped as a formula's spill would be, and nothing filled them
again. The range of such a cell is now forgotten when it is read: the anchor
and the cells of its spill keep their cached values as plain values, as the
file shows them, with no live spill. An ordinary formula's dynamic-array spill
is handled as before.
2026-11-01 12:56:55 -05:00
prxnvt 113aeff07c XLSX: a damaged Python part is a warning, and the workbook opens with its values
A Python part whose zip entry failed to read (a bad checksum or compressed
stream) made the whole workbook fail to open: the error was passed up, and the
failed read had also ended the package's read budget, so even a warning would
have left the sheets unread. The part is now read with
Package::read_optional_limited, which charges a failed read only that part's
allowance, and every error other than TooLarge is a warning. The Python cells
then keep only their cached values, as when the part is missing. Malformed XML
in the part was already a warning; the test covers it too.
2026-11-01 12:36:04 -05:00
prxnvt 44e9ddad45 Engine: a Python result cut by the adoption budget takes its cached spill with it
When a file's cached Python spills were larger than the adoption budget, the
cells past it kept only their own value, but their spill ranges and spilled
values stayed on the sheet, which went on showing values no result held. The
spill of such a cell is now cleared, as for a dropped result; the warning is
unchanged.
2026-11-01 12:33:22 -05:00
prxnvt fcbbfa5142 XLSX: Python cells as _xlfn._xlws.PY() formulas, code in the Python part, cached values; opening limits
A Python cell is written to xlsx as [MS-XLSX] 2.3.10 specifies: its
formula is _xlfn._xlws.PY(i,0|1), where i is the zero-based index of the
cell's script in the workbook's Python part and 0|1 the return type, 0
for an Excel value and 1 for a Python object. The part (2.1.23) is
xl/python.xml, content type application/vnd.ms-excel.python+xml,
related from the workbook part with
http://schemas.microsoft.com/office/2023/09/relationships/Python; its
root python holds pythonScripts, one pythonScript per Python cell in the
order the cells are written, with the code in code. The code keeps its
xl() references as text, so the formula has no further arguments. The
cell keeps its cached value. A spilling result is written like a dynamic
array (cm="1", t="array" ref=...) with the spilled values cached, so
other applications show the values (and #NAME? if they recalculate).

The reader turns a PY( formula with or without the _xlfn. and _xlws.
prefixes into a Python cell. An index names a script of the Python part,
and each %Px% in the script becomes the text of the formula's argument x
(2.3.10.4). The string-literal form, PY("chunk"&"chunk",0|1), still
reads, from the formula's own text so line breaks in the code survive. A
formula naming a script the part doesn't hold keeps only its cached
value, with a warning: no calculation can evaluate it. The reader keeps
the cached spilled values as the cell's result, so a workbook from Excel
shows its arrays without running anything. Python errors are cached as
#VALUE!, as #SPILL! and #CALC! already are. Native JSON keeps Cell.py
whole: code, return type, the last result and the exact error.

Calc lays out a Python cell's kept result: Python cells are nodes of the
dependency graph that read nothing, and a pass takes a dirty Python
cell's result instead of evaluating it, so its array spills, is blocked
(#SPILL!) and unblocked, and feeds its dependents exactly like a
formula's. An array that is malformed or has more than 1,000,000 cells
(only a crafted file holds one) shows #SPILL!. A dirty Python cell is
pending in the pass, as a dirty formula is, so a formula, a range read
or a cell-by-cell range walk that reaches it before its turn lays it out
first rather than reading its old value.

Opening a file can't make the app loop over a claimed size: the reader
keeps a Python cell's spill range only when it has at most 1,000,000
cells (a range claiming the whole sheet is dropped with its values, as a
formula's spilled values are), and io::adopt_python rebuilds results for
at most 4,000,000 cells over the whole workbook, with a warning for the
cells it skips. The Python part is read up to 64 MB and 1,000,000
scripts, a script is kept up to one character past what a cell may
hold, and the cells take at most 64 MB of code from the part in all,
since several formulas can name one script. io::enforce_python_limits
drops the code of a cell whose code doesn't fit in 8,192 characters as
PY(...) (the cell keeps its cached value, with a warning) and a kept
result that isn't a well-formed array within the cap, each with its
spill range and spilled values, so the sheet and a save no longer show
the old spill. Formats that keep PY() as an ordinary formula (xls, xlsb,
ods) are adopted the same way. Opening a file runs nothing.

The command-sequence fuzzer sets Python cells (code with xl()
references, quotes, line breaks, and code at and over the size limit),
and its xlsx round-trip check allows the Python errors becoming
#VALUE!, which is what the file format stores.

Tests: the written formula naming its script, the Python part with its
content type and relationship, return types, a spilling table saved as a
dynamic array, Python errors as #VALUE!, the round trip of values, code,
long code with quotes and line breaks and the error, and no Python part
without Python cells; a workbook laid out as the format specifies (an
index, a parameter named as %P2%) read as Python cells with their
spilled values and saved back with its code in a Python part; the
string-literal form; a formula naming a missing script, with and without
a Python part; 20,000 cells naming one long script against the code
budget; the part's reader and writer (escaping, odd %P tokens, a long
script, a repeated parameter); a blocker in the spill area giving #SPILL!
and clearing it spilling again; the native JSON round trip; a spill
range claiming the whole sheet; the workbook-wide budget; hostile JSON
(code over the limit, a result claiming far more cells than it holds); a
malformed result that never spills; PY() formulas at 8,079 and 8,080
characters of code. Calc: formulas, MAX, SUM, COUNTA and SUMPRODUCT read
a dirty Python cell whose array is blocked before its turn, in a full
and an incremental recalculation, and see the array once it is
unblocked; enforce_python_limits removes the spill of dropped code and
of a dropped result, and keeps a sound one.
2026-10-26 10:18:38 -04:00