From 246cf4e7f16e12a813985c4fbe450d863e8b041f Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Thu, 30 Nov 2023 23:12:43 +0100 Subject: [PATCH] RasterBand::histogram(): error out if n_buckets == 0 to avoid crash in GDAL --- src/raster/rasterband.rs | 4 ++++ src/raster/tests.rs | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/raster/rasterband.rs b/src/raster/rasterband.rs index c4d76ff7..09beba0a 100644 --- a/src/raster/rasterband.rs +++ b/src/raster/rasterband.rs @@ -901,6 +901,10 @@ impl<'a> RasterBand<'a> { include_out_of_range: bool, is_approx_ok: bool, ) -> Result { + if n_buckets == 0 { + return Err(GdalError::BadArgument("n_buckets should be > 0".to_string())); + } + let mut counts = vec![0; n_buckets]; let rv = unsafe { diff --git a/src/raster/tests.rs b/src/raster/tests.rs index 152ca7f7..bbfc4e7f 100644 --- a/src/raster/tests.rs +++ b/src/raster/tests.rs @@ -782,6 +782,10 @@ fn test_raster_histogram() { .map(|i| expected[i] + expected[i + 1]) .collect::>(); assert_eq!(hist.counts(), &expected_small); + + // n_buckets = 0 is not allowed + let hist = rb.histogram(-0.5, 255.5, 0, true, true); + hist.expect_err("histogram with 0 buckets should panic"); } #[test]