diff --git a/src/decoder/image.rs b/src/decoder/image.rs index c84d73f..6a4fc65 100644 --- a/src/decoder/image.rs +++ b/src/decoder/image.rs @@ -97,6 +97,11 @@ impl Image { let width = tag_reader.require_tag(Tag::ImageWidth)?.into_u32()?; let height = tag_reader.require_tag(Tag::ImageLength)?.into_u32()?; + if width == 0 || height == 0 { + return Err(TiffError::FormatError(TiffFormatError::InvalidDimensions( + width, height, + ))); + } let photometric_interpretation = tag_reader .find_tag(Tag::PhotometricInterpretation)? diff --git a/src/encoder/mod.rs b/src/encoder/mod.rs index 9886ae7..6e39c93 100644 --- a/src/encoder/mod.rs +++ b/src/encoder/mod.rs @@ -13,6 +13,7 @@ use std::{ use crate::{ error::TiffResult, tags::{CompressionMethod, ResolutionUnit, Tag}, + TiffError, TiffFormatError, }; pub mod colortype; @@ -349,6 +350,12 @@ impl<'a, W: 'a + Write + Seek, T: ColorType, K: TiffKind, D: Compression> height: u32, compression: D, ) -> TiffResult { + if width == 0 || height == 0 { + return Err(TiffError::FormatError(TiffFormatError::InvalidDimensions( + width, height, + ))); + } + let row_samples = u64::from(width) * u64::try_from(::BITS_PER_SAMPLE.len())?; let row_bytes = row_samples * u64::from(::BYTE_LEN); diff --git a/src/error.rs b/src/error.rs index 8c8ac8b..5653d00 100644 --- a/src/error.rs +++ b/src/error.rs @@ -56,6 +56,7 @@ pub enum TiffFormatError { actual_samples: usize, required_samples: usize, }, + InvalidDimensions(u32, u32), InvalidTag, InvalidTagValueType(Tag), RequiredTagNotFound(Tag), @@ -100,6 +101,7 @@ impl fmt::Display for TiffFormatError { actual_samples, required_samples ) } + InvalidDimensions(width, height) => write!(fmt, "Invalid dimensions: {}x{}.", width, height), InvalidTag => write!(fmt, "Image contains invalid tag."), InvalidTagValueType(ref tag) => { write!(fmt, "Tag `{:?}` did not have the expected value type.", tag)