DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Tables: a shape a table cannot hold is clamped, not allocated (#1583)
Hamzab49535624c6b
diff --git a/crates/doc/src/table.rs b/crates/doc/src/table.rsindex d5342b5c9..937fefe1f 100644--- a/crates/doc/src/table.rs+++ b/crates/doc/src/table.rs@@ -25,6 +25,32 @@ mod story;pub use cell::{AltFills, AltStrokes, Cell, CellStroke, Column, Row, RowHeightMode, RowKind, TableOptions};pub use grid::CellRange;+/// Most rows a table holds, as InDesign allows.+pub const MAX_TABLE_ROWS: usize = 10_000;+/// Most columns a table holds, as InDesign allows.+pub const MAX_TABLE_COLS: usize = 1_000;+/// Most cells a table holds, whatever its shape.+///+/// A [`Cell`] is about 1.4 KB (it carries a [`Story`]), so this is roughly 340 MB of cells at the+/// limit. The row and column limits alone are not enough: 10,000 × 1,000 would be 13.6 GB, and an+/// IDML story holding one `<Table>` with 5,000 `<Row/>` and 5,000 `<Column/>` -- about 1 KB+/// zipped -- asked for 25M cells, some 34 GB. That allocation does not fail politely: it aborts+/// the process through `handle_alloc_error`, which no `catch_unwind` can save, so every open+/// document's unsaved work goes with it (#1583).+pub const MAX_TABLE_CELLS: usize = 250_000;++/// The rows and columns a table of `rows × cols` is actually given, inside the limits above.+///+/// Columns are clamped first and rows take what is left of the cell budget, because a table is+/// far more often long than wide: truncating a 5,000-column table to 1,000 columns and then its+/// rows loses less of it than the other way round. Callers that read a file report the+/// difference; the counts are a document's claim, not something to trust.+pub fn clamped_size(rows: usize, cols: usize) -> (usize, usize) {+ let cols = cols.clamp(1, MAX_TABLE_COLS);+ let rows = rows.clamp(1, MAX_TABLE_ROWS.min(MAX_TABLE_CELLS / cols).max(1));+ (rows, cols)+}+#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]#[serde(rename_all = "camelCase")]pub struct Table {@@ -48,6 +74,48 @@ mod tests {Story::with_text(StoryId(1), t, ParaFormat::default())}+ /// A table's shape comes out of IDML and DOCX files unchecked, and `Cell` is about 1.4 KB, so+ /// a file claiming 5,000 x 5,000 asked for 25M cells -- some 34 GB. That allocation aborts the+ /// process rather than failing, taking every open document's unsaved work with it (#1583).+ #[test]+ fn a_table_larger_than_the_limits_is_clamped_not_allocated() {+ // The reported shape: 5,000 rows and 5,000 columns.+ let t = Table::new(1, 5_000, 5_000, 0, 0, 500.0);+ assert_eq!(t.columns.len(), MAX_TABLE_COLS, "columns are clamped to InDesign's limit");+ assert!(t.cells.len() <= MAX_TABLE_CELLS, "{} cells is within the budget", t.cells.len());+ assert_eq!(t.rows.len() * t.columns.len(), t.cells.len(), "the grid stays rectangular");+ // What it would have been: 25,000,000 cells at 1,360 bytes each.+ assert!(25_000_000 * std::mem::size_of::<Cell>() > 30_000_000_000, "the size this test is about");+ // Freed before the next large table, so the peak stays at one of them.+ drop(t);++ // A long table keeps its rows while it fits the budget (the budget itself is checked by+ // `clamped_size`, which allocates nothing).+ let long = Table::new(2, 10_000, 5, 0, 0, 500.0);+ assert_eq!((long.rows.len(), long.columns.len()), (10_000, 5), "a long thin table is untouched");+ assert_eq!(clamped_size(10_000, 25), (10_000, 25), "10,000 x 25 is inside the budget");+ drop(long);++ // Past the row limit the rows are clamped too.+ let longer = Table::new(3, 50_000, 2, 0, 0, 100.0);+ assert_eq!(longer.rows.len(), MAX_TABLE_ROWS, "rows are clamped to InDesign's limit");+ drop(longer);++ // Header, body and footer share the budget, and the table still has a body.+ let with_parts = Table::new(4, 5_000, 5_000, 3, 2, 500.0);+ assert!(with_parts.rows.len() <= MAX_TABLE_ROWS, "{} rows", with_parts.rows.len());+ assert!(with_parts.cells.len() <= MAX_TABLE_CELLS, "{} cells", with_parts.cells.len());+ assert!(with_parts.rows.iter().any(|r| r.kind == RowKind::Header), "the header survives");+ assert!(with_parts.rows.iter().any(|r| r.kind == RowKind::Footer), "the footer survives");+ assert!(with_parts.rows.iter().any(|r| r.kind == RowKind::Body), "and so does a body row");+ drop(with_parts);++ // A header and footer bigger than the whole budget still leave a body row.+ let all_header = Table::new(6, 1, 1, MAX_TABLE_ROWS * 2, MAX_TABLE_ROWS * 2, 100.0);+ assert!(all_header.rows.len() <= MAX_TABLE_ROWS, "{} rows", all_header.rows.len());+ assert!(all_header.rows.iter().any(|r| r.kind == RowKind::Body), "a body row survives");+ }+#[test]fn move_row_keeps_header_and_footer_rows_in_place() {let mut t = Table::new(1, 3, 2, 1, 1, 120.0);diff --git a/crates/doc/src/table/grid.rs b/crates/doc/src/table/grid.rsindex 48647cffc..f93379231 100644--- a/crates/doc/src/table/grid.rs+++ b/crates/doc/src/table/grid.rs@@ -25,8 +25,16 @@ impl CellRange {impl Table {/// A `rows × cols` table (`header` + body + `footer` rows) whose columns share `width`.+ ///+ /// The shape is clamped to [`clamped_size`](super::clamped_size): the counts reach this from+ /// IDML and DOCX files, where they are whatever the file says (#1583).pub fn new(id: u64, body_rows: usize, cols: usize, header: usize, footer: usize, width: f64) -> Table {- let cols = cols.max(1);+ // Header, body and footer rows share one budget: the total is clamped first, then the+ // header and footer take what they can of it, and the body keeps at least one row.+ let (total, cols) = super::clamped_size(body_rows.max(1).saturating_add(header).saturating_add(footer), cols);+ let header = header.min(total.saturating_sub(1));+ let footer = footer.min(total.saturating_sub(header).saturating_sub(1));+ let body_rows = total.saturating_sub(header).saturating_sub(footer).max(1);let mut rows = Vec::new();rows.extend((0..header).map(|_| Row { kind: RowKind::Header, ..Default::default() }));rows.extend((0..body_rows.max(1)).map(|_| Row::default()));diff --git a/crates/idml/src/import/table.rs b/crates/idml/src/import/table.rsindex 5e5a8685d..9bf0f920f 100644--- a/crates/idml/src/import/table.rs+++ b/crates/idml/src/import/table.rs@@ -25,8 +25,21 @@ impl<'r> Importer<'r> {let id = self.alloc();let rows: Vec<&El> = e.find_all("Row").collect();let cols: Vec<&El> = e.find_all("Column").collect();- let nr = rows.len().max(1);- let nc = cols.len().max(1);+ // A file's row and column counts are its claim, and a table of them is allocated whole: a+ // story holding one `<Table>` with 5,000 of each asked for 25M cells, around 34 GB, which+ // aborts the process instead of failing (#1583). What is read is clamped, and the rest of+ // this function walks the clamped slices -- indexing `t.rows`/`t.columns` by the file's+ // own counts would be out of bounds.+ let (nr, nc) = designcraft_doc::table::clamped_size(rows.len().max(1), cols.len().max(1));+ if nr < rows.len() || nc < cols.len() {+ self.warnings.push(format!(+ "a table of {} rows and {} columns was read as {nr} x {nc}: more than a table can hold",+ rows.len(),+ cols.len()+ ));+ }+ let rows = &rows[..nr.min(rows.len())];+ let cols = &cols[..nc.min(cols.len())];let mut t = Table::new(id, nr, nc, 0, 0, 0.0);t.options.direction = if e.get("TableDirection") == Some("RightToLeftDirection") {designcraft_doc::TextDirection::RightToLeft