Skip to content

Commit

Permalink
Address review, polish #2340 empty check
Browse files Browse the repository at this point in the history
  • Loading branch information
HeroicKatora committed Oct 12, 2024
1 parent 15b6e87 commit e62349b
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/imageops/sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ pub(crate) fn box_kernel(_x: f32) -> f32 {
// ```new_width``` is the desired width of the new image
// ```filter``` is the filter to use for sampling.
// ```image``` is not necessarily Rgba and the order of channels is passed through.
//
// Note: if an empty image is passed in, panics unless the image is truly empty.
fn horizontal_sample<P, S>(
image: &Rgba32FImage,
new_width: u32,
Expand All @@ -231,6 +233,13 @@ where
S: Primitive + 'static,
{
let (width, height) = image.dimensions();
// This is protection against a memory usage similar to #2340. See `vertical_sample`.
assert!(
// Checks the implication: (width == 0) -> (height == 0)
width != 0 || height == 0,
"Unexpected prior allocation size. This case should have been handled by the caller"
);

let mut out = ImageBuffer::new(new_width, height);
let mut ws = Vec::new();

Expand Down Expand Up @@ -476,6 +485,7 @@ where
// This is protection against a regression in memory usage such as #2340. Since the strategy to
// deal with it depends on the caller it is a precondition of this function.
assert!(
// Checks the implication: (height == 0) -> (width == 0)
height != 0 || width == 0,
"Unexpected prior allocation size. This case should have been handled by the caller"
);
Expand Down Expand Up @@ -1294,4 +1304,14 @@ mod tests {
let result = resize(&empty, 256, 256, FilterType::Lanczos3);
assert!(result.into_raw().into_iter().all(|c| c == 0));
}

#[test]
fn issue_2340_refl() {
// Tests the swapped coordinate version of `issue_2340`.
let empty = crate::GrayImage::from_raw(0, 1 << 31, vec![]).unwrap();
let result = resize(&empty, 1, 1, FilterType::Lanczos3);
assert!(result.into_raw().into_iter().all(|c| c == 0));
let result = resize(&empty, 256, 256, FilterType::Lanczos3);
assert!(result.into_raw().into_iter().all(|c| c == 0));
}
}

0 comments on commit e62349b

Please sign in to comment.