Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Merge pull request #1868 from Hamza0101/fix/table-allocation-cap

Brandon Thomascddf074cc485
diff --cc crates/idml/src/import/table.rs
index 5b5ca744b,91ac287f9..268f1af97
--- a/crates/idml/src/import/table.rs
+++ b/crates/idml/src/import/table.rs
@@@ -1,6 -1,39 +1,39 @@@
use super::values::{span, tint};
-use super::*;
+use super::{CellRange, El, Importer, RowHeightMode, StoryId, Table, names, swatch};
+ /// Cells every table in one import may hold between them. A `Cell` carries a whole story, so
+ /// it is 1,360 bytes: 400,000 of them is about 520 MB, which is the 512 MiB the package reader
+ /// already allows one part of the file to decompress to, and the same count the spreadsheet
+ /// importer collects at most.
+ const MAX_IMPORT_TABLE_CELLS: usize = 400_000;
+
+ /// Clamp warnings one import writes before it says the rest were the same.
+ const MAX_TABLE_WARNINGS: usize = 20;
+
+ /// How large a table of `rows` x `cols` may be read when `reserved` cells have already been
+ /// taken, and whether the file-wide budget is what cut it down -- or `None` when the budget is
+ /// spent and the table is not read at all.
+ ///
+ /// The per-table cap leaves a file free to hold many capped tables, so each is also clamped
+ /// against what is left. While there is budget, one row and one column are always kept: the table
+ /// is then still in the document, still says where its text went, and a file pays one cell for a
+ /// table it really does contain.
+ ///
+ /// Past that there is nothing left to pay with. Keeping a 1 x 1 table for every further `<Table>`
+ /// was unbounded in the one direction the budget was meant to close: `<Table></Table>` is fifteen
+ /// bytes, so the 512 MiB a part may decompress to holds some 36 million of them, and a `Cell`
+ /// each is tens of gigabytes with every table inside both caps (noted in review).
+ fn budgeted_size(rows: usize, cols: usize, reserved: usize) -> Option<(usize, usize, bool)> {
+ let (mut nr, mut nc) = designcraft_doc::table::clamped_size(rows, cols);
+ let left = MAX_IMPORT_TABLE_CELLS.checked_sub(reserved).filter(|left| *left > 0)?;
+ let over_budget = nr.saturating_mul(nc) > left;
+ if over_budget {
+ nc = nc.min(left);
+ nr = nr.min((left / nc).max(1));
+ }
+ Some((nr, nc, over_budget))
+ }
+
/// Preserve the legacy uniform field only when all four sides explicitly specify every
/// property. Partial records must stay sparse so they can inherit from a parent style.
pub(super) fn uniform_cell_stroke(edges: &[designcraft_doc::CellStrokeAttrs; 4]) -> Option<designcraft_doc::CellStroke> {