Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected panic due to unwrap None in JpegEncoder::encode #2018

Closed
thdusdl1219 opened this issue Sep 24, 2023 · 2 comments
Closed

Unexpected panic due to unwrap None in JpegEncoder::encode #2018

thdusdl1219 opened this issue Sep 24, 2023 · 2 comments

Comments

@thdusdl1219
Copy link

This happens in

L458, 463, 468, 473

pub fn encode(
&mut self,
image: &[u8],
width: u32,
height: u32,
color_type: ColorType,
) -> ImageResult<()> {
assert_eq!(
(width as u64 * height as u64).saturating_mul(color_type.bytes_per_pixel() as u64),
image.len() as u64
);
match color_type {
ColorType::L8 => {
let image: ImageBuffer<Luma<_>, _> =
ImageBuffer::from_raw(width, height, image).unwrap();
self.encode_image(&image)
}
ColorType::La8 => {
let image: ImageBuffer<LumaA<_>, _> =
ImageBuffer::from_raw(width, height, image).unwrap();
self.encode_image(&image)
}
ColorType::Rgb8 => {
let image: ImageBuffer<Rgb<_>, _> =
ImageBuffer::from_raw(width, height, image).unwrap();
self.encode_image(&image)
}
ColorType::Rgba8 => {
let image: ImageBuffer<Rgba<_>, _> =
ImageBuffer::from_raw(width, height, image).unwrap();
self.encode_image(&image)
}

Expected

return Result instead of panic?

Actual behaviour

thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', .cargo/registry/src/index.crates.io-6f17d22bba15001f/image-0.24.7/./src/codecs/jpeg/encoder.rs:449

Reproduction steps

    let mut buffer = Vec::new();
    image::ImageEncoder::write_image(image::codecs::jpeg::JpegEncoder::new(&mut
    buffer), &[0], 1, 1024, image::ColorType::L8 /* , La8, Rgb8, or Rgba8 */ );
@fintelia
Copy link
Contributor

Could you double check that this still happens on master? I think the assertion on line 450 (added in #2008) should now catch this case

@thdusdl1219
Copy link
Author

thdusdl1219 commented Sep 24, 2023

Right, it does not happen on master after the fix. Please feel free to close this issue, Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants