Compare commits

..
Author SHA1 Message Date
Dylan M. Taylor 3982596322 Merge branch 'main' into fix/sec-ods-row-text-budget 2026-11-03 17:34:37 -05:00
Dylan M. Taylor 01dee46561 ods: charge a cell's text when the cell is read, not when its row ends
The importer kept every cell of a row and only charged their text to
the 64 MiB allowance when the row finished, so one row of long cells
could hold up to 16,384 MiB before being refused. Text is now charged
as each cell finishes; the row charges only its repeats.
2026-11-03 11:15:58 +00:00
4 changed files with 36 additions and 22 deletions
+14 -3
View File
@@ -575,9 +575,20 @@ pub fn series(cx: &mut Ctx, sheet: usize, options: SeriesOptions<'_>) -> Result<
match (unit, d) {
("month", Some(_)) => (step as i64).checked_mul(i as i64).and_then(|m| add_months_clamped(sys, start, m)).unwrap_or(v),
("year", Some(_)) => (step as i64).checked_mul(i as i64 * 12).and_then(|m| add_months_clamped(sys, start, m)).unwrap_or(v),
// Whole weeks at a time: a huge step costs no more than a small one.
("weekday", Some(_)) => add_weekdays(sys, v, step as i64)
.ok_or_else(|| crate::EngineError::Other("The weekday series runs past the supported date range.".into()))?,
("weekday", Some(_)) => {
let mut n = v;
let mut left = step.abs() as i64;
while left > 0 {
n += step.signum();
let Some(day) = datetime_from_serial(sys, n) else {
return Err(crate::EngineError::Other("The weekday series runs past the supported date range.".into()));
};
if day.weekday != 0 && day.weekday != 6 {
left -= 1;
}
}
n
}
_ => v + step,
}
}
-18
View File
@@ -3975,24 +3975,6 @@ fn fill_series_weekday_out_of_range_step_errors() {
assert_eq!(v(&s, "A2"), Value::Empty);
}
/// A step of a million weekdays moves whole weeks at a time instead of visiting every day, and
/// lands where counting day by day would.
#[test]
fn fill_series_weekday_large_step_is_arithmetic() {
let mut s = s();
// 45005 is Monday 20 March 2023; five weekdays on from a Monday is the next Monday.
s.execute("cell.set", json!({"cell": "A1", "input": "45005"})).unwrap();
s.execute("edit.fillSeries", json!({"range": "A1:A3", "direction": "columns", "type": "date", "dateUnit": "weekday", "step": 1_000_000}))
.unwrap();
assert_eq!(v(&s, "A2"), Value::Number(45005.0 + 1_400_000.0));
assert_eq!(v(&s, "A3"), Value::Number(45005.0 + 2_800_000.0));
s.execute("cell.set", json!({"cell": "B1", "input": "45009"})).unwrap();
s.execute("edit.fillSeries", json!({"range": "B1:B3", "direction": "columns", "type": "date", "dateUnit": "weekday", "step": -1})).unwrap();
// Friday 24 March 2023 back one weekday at a time: Thursday, Wednesday.
assert_eq!(v(&s, "B2"), Value::Number(45008.0));
assert_eq!(v(&s, "B3"), Value::Number(45007.0));
}
#[test]
fn fill_series_month_leap_year() {
let s = series_date("2028-01-31", "A1:A3", "month");
+10 -1
View File
@@ -470,6 +470,14 @@ impl Import {
None => return Err(IoError::TooLarge("ODS row exceeds worksheet width".into())),
};
if kept {
// Charge the text a row keeps before the row is done, so a row of many long cells is
// refused as it grows; finish_row charges the row's repeats beyond the first.
if let Value::Text(t) = &cell.value {
self.text = self.text.saturating_add(u64::from(draft.repeat).saturating_mul(t.len() as u64));
if self.text > MAX_TEXT {
return Err(IoError::TooLarge("ODS expands beyond the import limit (64 MiB of text)".into()));
}
}
row.cells.push(RepeatedCell { col: row.col, repeat: draft.repeat, width, height, cell });
}
row.col = end;
@@ -537,7 +545,8 @@ impl Import {
if !entry.cell.is_blank() {
self.cells = self.cells.saturating_add(count);
if let Value::Text(t) = &entry.cell.value {
self.text = self.text.saturating_add(count.saturating_mul(t.len() as u64));
let repeats = count.saturating_sub(u64::from(entry.repeat));
self.text = self.text.saturating_add(repeats.saturating_mul(t.len() as u64));
}
}
let merged = entry.width > 1 || entry.height > 1;
+12
View File
@@ -200,6 +200,18 @@ fn ods_expansion_bounds_stop_small_repeat_bombs() {
"x".repeat(1024)
);
assert!(matches!(read_ods(&ods(&text)), Err(IoError::TooLarge(_))));
// One row of cells each just under the 1 MiB cell limit, past 64 MiB in all: refused while
// the row is read, before it holds them all. The row's end is never reached.
let long = format!(r#"<table:table-cell office:value-type="string" office:string-value="{}"/>"#, "x".repeat(1024 * 1024 - 1));
let cells = ods(&format!(r#"<table:table table:name="Data"><table:table-row>{}"#, long.repeat(65)));
assert!(matches!(read_ods(&cells), Err(IoError::TooLarge(_))));
// A row repeated once more than its cells' text allows is refused too, and one that fits reads.
let cell = format!(r#"<table:table-cell office:value-type="string" office:string-value="{}"/>"#, "y".repeat(1024));
let rows = |n: u32| {
ods(&format!(r#"<table:table table:name="Data"><table:table-row table:number-rows-repeated="{n}">{cell}</table:table-row></table:table>"#))
};
assert!(read_ods(&rows(65_536)).is_ok());
assert!(matches!(read_ods(&rows(65_537)), Err(IoError::TooLarge(_))));
}
#[test]