← Blog
How it works TableRuneFundQuill

A cell reference is a size claim, and the file can lie

“A customer sent us a spreadsheet that is about one and a half kilobytes and our importer died on it. Not a parse error, it just went away. The same code reads a forty megabyte file from the same customer fine, so I cannot work out what about this one is expensive.”

On 24 October someone filed an issue against a web app's Excel importer saying that a crafted workbook could make the parser allocate memory in proportion to a cell's address rather than to the size of the file. The issue is fderuiter/QRCraftly-web#1584, opened and closed as completed the same day, fixed by PR #1626.

It is a small finding in a small crate, and the fix is short. What makes it worth a post is that the same shape turns up in readers that have nothing to do with each other, that one of the cases needs no crafting at all, and that the sharpest version of the failure is not a crash but a wrong number sitting quietly in column A. We rebuilt all of it ourselves, and some of what we measured is worse than what the issue reports.

Where the cost comes from

A sheet inside an xlsx file is XML, and each cell carries its own address:

<sheetData>
  <row r="1">
    <c r="A1" t="n"><v>1</v></c>
    <c r="B1" t="n"><v>2</v></c>
  </row>
</sheetData>

The r attribute is the A1-style reference you would type into a formula bar. Nothing in the file says how many cells there are, and absent cells are simply not written, so a sheet with two values in opposite corners is a handful of bytes.

That is the on-disk shape. The problem starts when a reader turns it into the thing application code wants, which is almost always a rectangle: a Vec<Vec<String>>, a list of lists, a dataframe, rows to hand to a CSV writer. Making a rectangle means filling in the gaps, and the only thing that says how wide the rectangle is, is the largest reference in the file. So the reference stops being an address and becomes a size.

The letters are the part that bites. Column names are a bijective base-26 numeral system: A is 1, Z is 26, AA is 27. Each extra letter multiplies the reachable number by 26, while each extra digit in the row number multiplies it by 10. Seven characters of digits buys you ten million rows. Seven characters of letters buys you this:

Reference Column number Note
A 1
XFD 16,384 the last column of an Excel worksheet
XFE 16,385 the first one past it
ZZZ 18,278
ZZZZZ 12,356,630
ZZZZZZ 321,272,406
ZZZZZZZ 8,353,082,582 larger than the largest unsigned 32-bit integer

Those are ours, computed from the numeral system rather than copied. The limit itself is Microsoft's: its specifications page gives the total number of rows and columns on a worksheet as "1,048,576 rows by 16,384 columns". The page does not name the last column; that 16,384 is XFD is the arithmetic above.

Replaying the loop

The code the issue points at is two nested while loops that pad out to the index. Here it is as it stood at commit 5d439b08, the parent of the fix, copied verbatim from crates/excel-parse/src/xml.rs:

let mut row_vec = Vec::new();
for (c_idx, val) in cells {
    while row_vec.len() < c_idx {
        row_vec.push(String::new());
    }

The loop bound is c_idx, and c_idx was read out of the file. We lifted both padding loops into a standalone binary, built them with the same compiler at -O, and ran them on a 16 GB x86_64 machine. One cell, one reference, nothing else:

Reference Entries pushed Time Peak RSS
A1 1 1 µs 2,144 KiB
XFD1 16,384 287 µs 2,620 KiB
ZZZZZ1 12,356,630 308 ms 291,848 KiB
ZZZZZZ1 321,272,406 5.54 s 7,532,056 KiB

The 12,356,630 matches the figure in the issue exactly, which is a good sign that we are measuring the same thing. The timings do not match: the issue reports about 207 ms where we see 308 ms, and it says plainly that its own numbers were not re-run during verification, so the difference is two machines and not a disagreement.

The bottom row is 7.18 GiB of resident memory, and the file it came from is 1,583 bytes. It is also almost exactly what the arithmetic predicts. An empty String in Rust is three machine words and nothing else, which we measured rather than assumed: 24 bytes on x86_64 and 12 bytes on wasm32. At 24 bytes, 321,272,406 of them is 7,710,537,744 bytes, and the measured peak was 7,712,825,344. The strings hold no characters. The memory is entirely the headers of empty ones.

The 4 GiB wall

QRCraftly's parser is compiled to WebAssembly and runs in the browser, which is where the loud version of this lives. A wasm32 module addresses its memory with 32-bit pointers, so its linear memory cannot exceed 4 GiB however much the machine has. At the 12 bytes a String occupies there, 321,272,406 of them is 3.59 GiB, and that is before the string data, before allocator overhead, and before the detail that makes it worse.

Vec grows by doubling. To reach a capacity of 321 million it passes through 268,435,456, and it has to allocate that whole buffer at once before copying into it. We ran the replayed loop again under a 4 GiB address-space limit, standing in for the wasm ceiling, and it did not get close:

memory allocation of 6442450944 bytes failed

That is a single 6 GiB request, from RawVec::grow_one, on behalf of a file of 1,583 bytes. The process aborts. In a browser the equivalent is the module trapping, which takes the whole wasm instance with it.

You do not need a crafted file

The issue is careful to say that real Excel files never exceed XFD, so only crafted files trigger it. That is true of its own parser, and it is where we stopped agreeing with the framing, because the expensive thing is not the illegal reference. It is the dense rectangle. And a perfectly legal reference is enough to ask for one that nobody can hold.

XFD1048576 is the last cell of an Excel worksheet. It is legal, and an ordinary writer produces it without complaint: openpyxl, given a value in A1 and one in XFD1048576, saved a 4,819-byte workbook containing <c r="XFD1048576"> and <dimension ref="A1:XFD1048576"/>. A rectangle from A1 to XFD1048576 is 17,179,869,184 cells.

We built a 1,592-byte workbook with a value in A1 and a value in XFD1048576, and gave it to three readers that share no code: openpyxl 3.1.5, python-calamine 0.9.1 wrapping the Rust calamine crate 0.37.0, and SheetJS 0.18.5.

calamine asked the allocator for 512 GiB and aborted:

memory allocation of 549755813888 bytes failed
  <calamine::Range<calamine::datatype::DataRef>>::from_sparse
  calamine::xlsx::Xlsx<...>::worksheet_range

549,755,813,888 is 17,179,869,184 multiplied by 32, and 32 is what we measured size_of::<calamine::Data>() to be. The number is not arbitrary. It is the rectangle.

SheetJS parsed the same file in 8 ms using 78 MB, because XLSX.read keeps cells in a sparse object keyed by address. Asking it for the rectangle, through sheet_to_json({header: 1}), is the step that does not come back: we stopped it after more than three minutes. On the merely large cases we could time it, the shape is clear. A single ZZZZZ1 cell took 17.9 s and 1.95 GiB to convert, from a 1,582-byte file.

openpyxl read the legal last cell in 2 ms and 23 MB, holding two cells, and reported the sheet as 1,048,576 rows by 16,384 columns without materialising any of it. It is sparse all the way through, and on this whole family of files it was never slow and never large.

The quiet one

Everything above is a crash, and crashes get fixed. The case that should worry you more produces no error at all.

Here is calamine's cell-reference parser, read from the crate source on disk at version 0.35.0:

let mut col: u32 = 0;
let mut row: u32 = 0;
while i < len {
    match range[i] {
        c @ b'A'..=b'Z' => col = col * 26 + (c - b'A') as u32 + 1,

col is a u32, the multiply has no bound, and in a release build it wraps instead of panicking. Seven letters can exceed 4,294,967,296, so seven letters can wrap, and a wrap lands somewhere. We worked out which letters land where, and then checked.

MWLQKWW is column 4,294,967,297, which is 2^32 plus one, which wraps to 1, which is column A. So we built a workbook whose <row r="1"> holds A1 with the value 11 and then MWLQKWW1 with the value 999, and read it with calamine 0.35.0. It reports a sheet one column wide, and the value in A1 is 999.

Reversing the document order, so the far cell comes first, gives a sheet one column wide whose A1 is 11. Both cells resolve to the same slot and the last one written wins. There is no warning, no error, and no trace in the output that two cells were ever involved.

We repeated it for four more targets, predicting each from the modulus before running it, and every prediction held:

Reference in the file True column Where the value landed
MWLQKWW1 4,294,967,297 column A
MWLQKWY1 4,294,967,299 column C
MWLQKXA1 4,294,967,301 column E
MWLQKXW1 4,294,967,323 column AA
MWLRJCZ1 4,294,983,680 column XFD

The last row is the one to sit with. MWLRJCZ1 resolves to column 16,384, the last legal column of the sheet. A reader that parses the reference and then checks whether the result is inside Excel's limits will pass this file, because by the time the check runs the number is inside the limits. The information that it was not has already been thrown away.

We saw the same wrap in QRCraftly's pre-fix function, for a second and independent reason: there usize is the accumulator, and on wasm32 usize is 32 bits. Compiled for wasm32-unknown-unknown and called from Node, the old function returns 0 for MWLQKWW and 16,383 for MWLRJCZ, exactly as calamine does on a 64-bit host. Two readers, two different type choices, the same silent collision.

Where the bound goes

The QRCraftly fix adds two constants and puts the check inside the accumulation loop:

index = index * 26 + digit;
if index > MAX_COLUMNS {
    return MAX_COLUMNS;
}

Its doc comment says why: a column past the limit "returns MAX_COLUMNS, so callers need only one bound check and the arithmetic cannot overflow." Checking inside the loop is what makes the second clause true. A check after the loop cannot help, because the value it would check has already wrapped. We rebuilt the fixed function for wasm32 and fed it the whole set: XFE, ZZZZZ, MWLQKWW and MWLRJCZ all come back as MAX_COLUMNS, and the cell is skipped.

calamine has a bound in the matching place and it does not help, which is the clearest illustration of the point. From from_sparse, read verbatim from the crate source:

let len = cols.saturating_mul(rows);
let mut v = vec![T::default(); len];

saturating_mul is there so the multiplication cannot overflow. It works. The product is correct, and the correct product is 17,179,869,184, and the next line asks for 512 GiB of it. The guard protects the arithmetic and not the allocation, and the allocation is the thing that fails. Elsewhere in the same file calamine reaches for wrapping_mul and wrapping_add explicitly in a different reference parser, so the overflow is understood in that codebase. It is the sheet-cell path that accumulates into a plain u32.

The three readers also disagree about what the limit is, which is its own small lesson. openpyxl refuses any column of four letters or more, with 'ZZZZZ' is not a valid column name. Column names are from A to ZZZ, which rejects the dangerous cases but also accepts XFE and ZZZ, both past Excel's last column. Its bound is the notation, not the grid. QRCraftly now bounds at XFD. calamine has no bound on the reference at all. The format does not settle it, so every reader picked something different.

What we measured, in one table

All on one machine on 2026-11-15, x86_64 with 16 GB, wasm32 under Node 22.23.4. Every input file is between 1,579 and 1,601 bytes.

File openpyxl 3.1.5 calamine 0.35.0, native calamine 0.35.0, wasm32 SheetJS 0.18.5, dense
A1, B1 3 ms, 23 MB 0.2 ms, 2.7 MB 1.4 MiB 11 ms
XFD1 2 ms, 23 MB 0.7 ms, 3.3 MB 2.4 MiB 22 ms
A3000000 2 ms, 23 MB 107 ms, 96 MB 70 MiB 3.6 s, 774 MB
ZZZZZ1 rejects 381 ms, 389 MB 284 MiB 17.9 s, 1.95 GiB
ZZZZZZ1 rejects 26.7 s, 9.58 GiB traps over 90 s, unfinished
XFD1048576 2 ms, 23 MB aborts, 512 GiB traps over 180 s, unfinished

The wasm32 column is linear memory after the read. "Traps" is RuntimeError: unreachable, which is how an abort inside a wasm module reaches the host. The row that matters most is the last one, because it is the only row in the table that an ordinary tool will produce for you.

Where TableRune fits

TableRune imports .xlsx, .xlsm, .xls and .ods by parsing them in the browser with SheetJS and converting the chosen sheet to CSV for DuckDB, on both of the app's file-drop paths. Reading that code at prod commit df979e6, the sheet extent is re-derived from the cells actually parsed rather than taken from the file's <dimension> declaration, and there are committed tests holding it there. That is the subject of an earlier post and we will not repeat it here.

What that code does not do is bound the reference. The extent comes from the largest cell it saw. The preview in the import dialog clamps it to 50 rows, and nothing clamps the columns. So we replayed the import call for call against SheetJS 0.20.3, the exact tarball TableRune's lockfile pins (its SHA-512 matches), under Node 24 on a 3 GB x86_64 machine. The time and memory are for opening the import dialog, which is the first thing that happens when a workbook is dropped:

File Opening the import dialog
A1, B1 12 ms, 78 MiB
A1, XFD1 29 ms, 84 MiB
A1, XFD1048576 0.47 s, 136 MiB
A1, ZZZZZ1 17.4 s, 1.85 GiB
A1, MWLQKWW1 out of memory after about 50 s

JavaScript does not wrap. SheetJS holds the column as an ordinary number, so in TableRune MWLQKWW1 really is column 4,294,967,297, and the preview tries to lay out a row that wide. That is the loud failure and not the quiet one, which is the better of the two, but it is still a 1.5 KB file that takes the import down. We measured it in Node and not in a browser.

The legal corner cell is the instructive one. Its preview is fast because the preview is clamped to 50 rows. The CSV conversion that follows is not clamped, and SheetJS writes every blank row of the range, 16,384 bytes of commas each, which we checked on a three-row file. For this workbook that is 17,179,869,184 bytes of CSV, 32 times the longest string Node 24's JavaScript engine allows (536,870,888 characters). We did not run that conversion to the end, so we are not saying how it fails, only that it cannot succeed.

All of that describes TableRune as it stood at df979e6, and we fixed it before publishing this. At prod commit 6ee57cc, a cell past column XFD or row 1,048,576 is deleted from the parsed sheet before anything else sees it, and the import dialog says how many were skipped. A sheet whose span would make more than 100 million CSV cells is refused before conversion, with a message naming the sheet and its range. We ran the shipped file against the same SheetJS build and the same workbooks. ZZZZZ1, MWLQKWW1 and XFE1 each open in under 60 ms with one cell reported skipped. XFD1 is kept. The corner-cell file previews in under half a second, and its conversion is refused within 4 ms.

Where FundQuill fits

FundQuill reads .xlsx financial models entirely on-device, in a Rust engine compiled to WebAssembly, and both of the ways it reads a workbook go through calamine. At prod commit da15e73 the lockfile pins calamine 0.35.0. Loading a file first calls read_dims, which calls worksheet_range and worksheet_formula on every sheet, and then hands the same bytes to the formula engine, formualizer-workbook 0.6.0, whose calamine backend reads the cell values through worksheet_range as well.

worksheet_range is the function in the traces above. So we built our own harness against that exact version, both native and for wasm32, and ran the same files through it. Both results reproduce. Natively, the 1,592-byte workbook whose last cell is XFD1048576 aborts with the 512 GiB request. On wasm32, where the whole address space is 4 GiB and the request is 128 times larger than that, the module traps with RuntimeError: unreachable. The wrapping case reproduces too: our harness reports one column, and the value that reaches column A is the one from MWLQKWW1.

Since read_dims runs first, the abort happens before the formula engine sees the file. The wrap aborts nothing, so in that case the formula engine does see it, through the same function.

We fixed it before publishing this, and the fix had to go around calamine rather than through it, because there is no way to bound its parse from outside. At prod commit 127baa6, FundQuill reads every sheet's XML itself before any calamine call, with its own reference parser that stops inside the loop at Excel's limits, and refuses the file with a plain message if anything would hurt. Building it turned up more than the cell address:

  • A legal XFD1 was enough. The formula engine sets up storage for every column, so on wasm32 a sheet 6,144 columns wide trapped. FundQuill now refuses sheets wider than 2,048 columns, which is 170 years of monthly columns.
  • A shared formula carries its own range, <f t="shared" ref="A1:XFD1048576">, and calamine's formula reader makes one hash-map entry for every cell in that range, whether or not those cells exist. A file whose only cells are A1 and B1 can ask for 17 billion entries.
  • calamine also sizes allocations from the shared-formula index si and from the shared-string count uniqueCount, straight from the attributes.

Each of those is now bounded, with a test that builds the hostile file by hand. The 6,144-column trap was measured while building the fix. For this post we checked each mechanism in the list against calamine's source rather than rerunning it.

If you read spreadsheets

  1. Treat the largest reference in a file as untrusted input that sizes an allocation, because that is what it is. The file is not a bound on it.
  2. Put the bound inside the parse, in the loop that accumulates the number. A check after the fact runs on a number that may already have wrapped, and wrapping can land inside your limits.
  3. Know whether your reader is sparse or dense, and where the conversion happens. SheetJS and openpyxl are cheap to parse; the cost is in the step that makes a rectangle, which is usually your own code asking for one. calamine makes the rectangle for you inside worksheet_range, so there is no cheap read to fall back on.
  4. Do not treat XFD1048576 as a hostile input. It is a legal reference to a real cell, and it is the one case here that an ordinary spreadsheet can hand you by accident.
  5. If you must have a dense grid, cap it at Excel's own limits and say so when you clamp. A skipped cell the user hears about is a different thing from a skipped cell they do not.
  6. If you guard a reader you do not control by scanning the file first, scan every number the reader will act on, and parse it the way the reader parses it. The cell address is only the most obvious size claim in an xlsx.

What this does not cover

Everything above is one failure shape measured on one machine. Specifically:

  • Excel itself was not run. The limits are quoted from Microsoft's specifications page; the name XFD for column 16,384 is the base-26 arithmetic, not the page.
  • The .xls binary format, .xlsb and ODS are not covered. All of the above is the zipped XML format and its readers.
  • The comparison table uses SheetJS 0.18.5 from npm. TableRune's own version, 0.20.3, was measured separately, on TableRune's import path and on a smaller machine, and its CSV conversion of the corner-cell file was not run to the end.
  • We did not read the issue's own repository beyond two source files, the issue and the pull request. The three findings in #1584 are related and we wrote about one.
  • No claim is made here about how often any of this happens in the wild. We have one public issue, our own two readers, and a pile of files we built ourselves.

The arithmetic, the measurements and the quoted source are all reproducible from the references above, and where we could not check something we have tried to say so in the sentence that would otherwise have claimed it.