diff --git a/crates/xlsx/src/chart.rs b/crates/xlsx/src/chart.rs index c9acf83d78..8c9f77f634 100644 --- a/crates/xlsx/src/chart.rs +++ b/crates/xlsx/src/chart.rs @@ -132,17 +132,22 @@ const MAX_CACHED_POINTS: usize = gridcraft_core::MAX_ROWS as usize; /// The points of a cache or literal (`c` holds `ptCount`, `pts` the ``s), by index; missing /// points are `None`. Points without a valid `idx` (schema-invalid, but some writers omit it) go /// in document order. A missing, unreadable or too small `ptCount` gives way to the points present. +/// +/// The per-chart budget is checked before allocation. If the requested count would exceed the +/// remaining budget, all present points are retained when they fit; only trailing gaps are +/// clamped. If present points extend past the remaining budget, points beyond that boundary are +/// dropped as well, and the caller is warned. fn read_points<'a>(c: &El, pts: &'a El, budget: &mut ChartReadBudget) -> Option>> { let at = |i: usize, p: &El| p.attr("idx").and_then(|v| v.trim().parse::().ok()).unwrap_or(i); let placed = || pts.kids("pt").enumerate().map(|(i, p)| (at(i, p), p)).filter(|(i, _)| *i < MAX_CACHED_POINTS); let highest = placed().map(|(i, _)| i + 1).max().unwrap_or(0); let requested = c.child_val("ptCount").and_then(|v| v.trim().parse::().ok()).map(|n| n.min(MAX_CACHED_POINTS).max(highest)).unwrap_or(highest); - let count = if requested > budget.remaining { - budget.exceeded = true; - highest.min(budget.remaining) - } else { + let count = if requested <= budget.remaining { requested + } else { + budget.exceeded = true; + if highest <= budget.remaining { highest } else { budget.remaining } }; budget.remaining = budget.remaining.saturating_sub(count); if count == 0 { @@ -438,7 +443,7 @@ fn axis_ids(g: &El) -> Vec { } /// A chart part, with the cached points of each series when it is a PivotChart (empty otherwise), -#[allow(dead_code)] +#[cfg(test)] pub(crate) fn read_chart(root: &El) -> Option<(Chart, Vec)> { read_chart_with_budget(root).map(|(chart, caches, _)| (chart, caches)) } diff --git a/crates/xlsx/src/drawing.rs b/crates/xlsx/src/drawing.rs index ce08d8466d..ebe00bc7a7 100644 --- a/crates/xlsx/src/drawing.rs +++ b/crates/xlsx/src/drawing.rs @@ -176,7 +176,7 @@ pub fn read_drawing(cx: &mut Ctx<'_>, part: &str, sheet: &mut Sheet) -> Result<( ch.id = cx.next_object_id(); ch.anchor = anchor; if budget_exceeded { - cx.warn("chart cached points exceeded the per-chart import budget; trailing gaps were truncated"); + cx.warn("cached chart points were dropped after the per-chart import budget was exceeded"); } if !caches.is_empty() { cx.pivot_charts.push((ch.id, caches)); diff --git a/crates/xlsx/src/tests/chart_read_tests.rs b/crates/xlsx/src/tests/chart_read_tests.rs index 5477d972e1..b9f82f1719 100644 --- a/crates/xlsx/src/tests/chart_read_tests.rs +++ b/crates/xlsx/src/tests/chart_read_tests.rs @@ -266,6 +266,19 @@ fn chart_cache_budget_bounds_many_empty_series() { assert!(warned); } +#[test] +fn chart_cache_budget_drops_points_beyond_boundary() { + let series: String = (0..17) + .map(|i| format!(r#"{i}"#)) + .collect(); + let root = crate::xml::parse(chart_xml(false, &format!("{series}"), "").as_bytes()).unwrap(); + let (chart, _, warned) = crate::chart::read_chart_with_budget(&root).unwrap(); + assert_eq!(chart.series.len(), 17); + assert!(chart.series.iter().take(16).all(|s| !s.values.is_empty())); + assert_eq!(chart.series[16].values, ""); + assert!(warned); +} + #[test] fn a_deleted_automatic_title_stays_deleted() { let one = ser("Vehicles", &str_cache("S!$A$1:$A$2", &["a", "b"]), &num_cache("S!$B$1:$B$2", 2, &[]));