From 48579215916e6ccb082bf96759978a094f872286 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 17 Nov 2026 10:45:39 -0800 Subject: [PATCH] craft-xls: the fixture writer refuses sheet counts the reader cannot read testing::workbook now returns craft_cfb::Error::Invalid before writing anything when it is given no sheets or more than 4,096 sheets, the counts craft_xls::read refuses. Before, it wrote a file that the reader then rejected as having no worksheets or too many sheets. --- crates/xls/src/read.rs | 2 +- crates/xls/src/testing.rs | 17 ++++++++++++++--- crates/xls/tests/read.rs | 19 +++++++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/crates/xls/src/read.rs b/crates/xls/src/read.rs index 8fdf5e473e..3820352e4b 100644 --- a/crates/xls/src/read.rs +++ b/crates/xls/src/read.rs @@ -16,7 +16,7 @@ use crate::records::{Cursor, Record, Records}; use crate::{Error, Limits, Read, Warning, cond, drawing, notes, styles}; const MAX_STRINGS: usize = 4_000_000; -const MAX_SHEETS: usize = 4096; +pub(crate) const MAX_SHEETS: usize = 4096; const MAX_NAMES: usize = 65_536; const MAX_XFS: usize = 65_536; const MAX_MERGES: usize = 100_000; diff --git a/crates/xls/src/testing.rs b/crates/xls/src/testing.rs index 1aaaf347e0..d57a5ee56f 100644 --- a/crates/xls/src/testing.rs +++ b/crates/xls/src/testing.rs @@ -54,13 +54,24 @@ fn number(cell: &str) -> Option<(u16, f64)> { /// Number records, a cell `date:` is a Number with the date format `m/d/yyyy` (built-in /// format 14), an empty cell is left out, and everything else, including a `date:` cell whose /// serial is not a number, goes through the shared-string table (as UTF-16 when it isn't -/// Latin-1). A nonempty cell past row 65,535 or column 255 (zero-based) returns -/// [`craft_cfb::Error::Invalid`] naming its sheet, row and column, and a compound file that cannot -/// be written returns the writer's error. +/// Latin-1). No sheets, or more than the 4,096 sheets [`crate::read()`] accepts, returns +/// [`craft_cfb::Error::Invalid`] naming the count. A nonempty cell past row 65,535 or column 255 +/// (zero-based) returns [`craft_cfb::Error::Invalid`] naming its sheet, row and column, and a +/// compound file that cannot be written returns the writer's error. /// /// shortcut: characters outside the Basic Multilingual Plane are dropped, sheet names are cut at /// 255 characters and strings at 4,000 (so each fits one record); lift when a fixture needs more. pub fn workbook(sheets: &[(&str, Vec>)]) -> Result, craft_cfb::Error> { + if sheets.is_empty() { + return Err(craft_cfb::Error::Invalid("fixture has no sheets; craft-xls reads a workbook only when it has a worksheet".into())); + } + if sheets.len() > crate::read::MAX_SHEETS { + return Err(craft_cfb::Error::Invalid(format!( + "fixture has {} sheets; craft-xls can read at most {}", + sheets.len(), + crate::read::MAX_SHEETS + ))); + } let mut sst: Vec<&str> = Vec::new(); let mut references = 0usize; for (name, rows) in sheets { diff --git a/crates/xls/tests/read.rs b/crates/xls/tests/read.rs index f4360b5e0c..93a984bded 100644 --- a/crates/xls/tests/read.rs +++ b/crates/xls/tests/read.rs @@ -634,6 +634,25 @@ fn the_fixture_writer_reads_back() { assert_eq!(v(&wb, 1, "A1"), text("Name")); } +/// A fixture holds 1 to 4,096 sheets, the counts the reader accepts; any other count is an error +/// naming it, returned before anything is written. +#[test] +fn the_fixture_writer_rejects_a_sheet_count_the_reader_refuses() { + let names: Vec = (0..4097).map(|i| format!("S{i}")).collect(); + let sheets: Vec<(&str, Vec>)> = names.iter().map(|n| (n.as_str(), Vec::new())).collect(); + match craft_xls::testing::workbook(&sheets) { + Err(craft_cfb::Error::Invalid(m)) => assert!(m.contains("4097 sheets") && m.contains("at most 4096"), "{m}"), + other => panic!("{:?}", other.map(|b| b.len())), + } + match craft_xls::testing::workbook(&[]) { + Err(craft_cfb::Error::Invalid(m)) => assert!(m.contains("no sheets"), "{m}"), + other => panic!("{:?}", other.map(|b| b.len())), + } + let read = craft_xls::read(&craft_xls::testing::workbook(&sheets[..4096]).unwrap()).unwrap(); + assert_eq!(read.warnings, []); + assert!(read.workbook.sheets.iter().map(|s| s.name.as_str()).eq(names[..4096].iter().map(String::as_str))); +} + #[test] fn a_fixture_that_cannot_be_written_returns_the_writer_error() { assert_eq!(craft_xls::testing::compound("", b"x"), Err(craft_cfb::Error::BadName(String::new())));