Skip to content

Commit

Permalink
RasterBand::histogram(): error out if n_buckets == 0 to avoid crash i…
Browse files Browse the repository at this point in the history
…n GDAL
  • Loading branch information
rouault authored and spadarian committed Dec 1, 2023
1 parent a7491be commit 246cf4e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/raster/rasterband.rs
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,10 @@ impl<'a> RasterBand<'a> {
include_out_of_range: bool,
is_approx_ok: bool,
) -> Result<Histogram> {
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 {
Expand Down
4 changes: 4 additions & 0 deletions src/raster/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,10 @@ fn test_raster_histogram() {
.map(|i| expected[i] + expected[i + 1])
.collect::<Vec<_>>();
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]
Expand Down

0 comments on commit 246cf4e

Please sign in to comment.