Skip to content

Commit b8a74a9

Browse files
Mavrick-Fclaude
andcommitted
fix(raster): refuse a band of the wrong length in write_all
write_all now returns an error when a band doesn't hold width × height values instead of filling the gap with NaN. The project round-trip test also checks that each derived layer's bands come from their own source bands. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 33dcedd commit b8a74a9

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

‎crates/data/src/raster/geotiff.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1740,13 +1740,16 @@ fn encode(r: &Raster, band: usize, every: bool) -> Result<Vec<u8>> {
17401740
// The samples interleaved, as large as the raster: reserved first, so a raster this
17411741
// machine can't hold twice is an error, and a cancel is heard while they are copied.
17421742
let n = (w as usize) * (h as usize);
1743+
if r.bands.iter().any(|b| b.data.len() != n) {
1744+
return Err(err(format!("a band of the raster doesn't hold its {w} × {h} values")));
1745+
}
17431746
let samples = n.checked_mul(all.len()).ok_or_else(|| err("the raster is too large to write"))?;
17441747
let mut data = super::room(samples, w as usize, h as usize)?;
17451748
for i in 0..n {
17461749
if i % (1 << 20) == 0 {
17471750
crate::progress::check()?;
17481751
}
1749-
// Every band holds `n` values (`Raster::new` checks), so none is NaN-filled.
1752+
// Every band holds `n` values (checked above), so none is NaN-filled.
17501753
data.extend(r.bands.iter().map(|b| b.data.get(i).copied().unwrap_or(f32::NAN)));
17511754
}
17521755
macro_rules! wide {

‎crates/engine/src/tests.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16621,6 +16621,12 @@ fn derived_multiband_rasters_keep_every_band_in_the_project() {
1662116621
};
1662216622
let before: Vec<_> = ["half", "part", "two"].iter().map(|name| bands(&s, name)).collect();
1662316623
assert!(before.iter().all(|b| b.len() == 2));
16624+
// Each derived band comes from its own source band: the first's values are under 16, the
16625+
// second's 100 or more (NaN where a cell has none).
16626+
for (name, bands) in ["half", "part"].iter().zip(&before) {
16627+
let values = |b: usize| bands[b].iter().map(|v| f32::from_bits(*v)).filter(|v| v.is_finite()).collect::<Vec<_>>();
16628+
assert!(!values(1).is_empty() && values(0).iter().all(|v| *v < 16.0) && values(1).iter().all(|v| *v >= 100.0), "{name}");
16629+
}
1662416630
// The edit set its pixel (the top-left one) in both bands and left the next one alone.
1662516631
let pixel = |band: usize, i: usize| before[2][band][i];
1662616632
assert_eq!([pixel(0, 0), pixel(1, 0), pixel(1, 1)], [1.0f32, 1.0, 101.0].map(f32::to_bits));

0 commit comments

Comments
 (0)