From ef3294d1fe195525ccc8249a392f3c2c28dfa812 Mon Sep 17 00:00:00 2001 From: Jonathan Behrens Date: Sun, 4 Sep 2022 12:33:10 -0700 Subject: [PATCH 1/4] Return an error for zero-size images --- src/decoder/image.rs | 3 +++ src/encoder/mod.rs | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/decoder/image.rs b/src/decoder/image.rs index c84d73f..61952a4 100644 --- a/src/decoder/image.rs +++ b/src/decoder/image.rs @@ -97,6 +97,9 @@ 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::InconsistentSizesEncountered)); + } let photometric_interpretation = tag_reader .find_tag(Tag::PhotometricInterpretation)? diff --git a/src/encoder/mod.rs b/src/encoder/mod.rs index 9886ae7..db6fa75 100644 --- a/src/encoder/mod.rs +++ b/src/encoder/mod.rs @@ -12,7 +12,7 @@ use std::{ use crate::{ error::TiffResult, - tags::{CompressionMethod, ResolutionUnit, Tag}, + tags::{CompressionMethod, ResolutionUnit, Tag}, TiffError, TiffFormatError, }; pub mod colortype; @@ -349,6 +349,10 @@ 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::InconsistentSizesEncountered)); + } + let row_samples = u64::from(width) * u64::try_from(::BITS_PER_SAMPLE.len())?; let row_bytes = row_samples * u64::from(::BYTE_LEN); From 945a02d1e1d7b45701e5a331ca22db630bc0bb5e Mon Sep 17 00:00:00 2001 From: Jonathan Behrens Date: Sun, 4 Sep 2022 12:46:11 -0700 Subject: [PATCH 2/4] Format --- src/decoder/image.rs | 4 +++- src/encoder/mod.rs | 7 +++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/decoder/image.rs b/src/decoder/image.rs index 61952a4..544ec7b 100644 --- a/src/decoder/image.rs +++ b/src/decoder/image.rs @@ -98,7 +98,9 @@ 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::InconsistentSizesEncountered)); + return Err(TiffError::FormatError( + TiffFormatError::InconsistentSizesEncountered, + )); } let photometric_interpretation = tag_reader diff --git a/src/encoder/mod.rs b/src/encoder/mod.rs index db6fa75..3547596 100644 --- a/src/encoder/mod.rs +++ b/src/encoder/mod.rs @@ -12,7 +12,8 @@ use std::{ use crate::{ error::TiffResult, - tags::{CompressionMethod, ResolutionUnit, Tag}, TiffError, TiffFormatError, + tags::{CompressionMethod, ResolutionUnit, Tag}, + TiffError, TiffFormatError, }; pub mod colortype; @@ -350,7 +351,9 @@ impl<'a, W: 'a + Write + Seek, T: ColorType, K: TiffKind, D: Compression> compression: D, ) -> TiffResult { if width == 0 || height == 0 { - return Err(TiffError::FormatError(TiffFormatError::InconsistentSizesEncountered)); + return Err(TiffError::FormatError( + TiffFormatError::InconsistentSizesEncountered, + )); } let row_samples = u64::from(width) * u64::try_from(::BITS_PER_SAMPLE.len())?; From fc0dafcde222a004d234e4c0dbd51e05813eb050 Mon Sep 17 00:00:00 2001 From: Jonathan Behrens Date: Mon, 5 Sep 2022 12:45:22 -0700 Subject: [PATCH 3/4] Change error kind --- src/decoder/image.rs | 2 +- src/encoder/mod.rs | 2 +- src/error.rs | 2 ++ 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/decoder/image.rs b/src/decoder/image.rs index 544ec7b..7adffc7 100644 --- a/src/decoder/image.rs +++ b/src/decoder/image.rs @@ -99,7 +99,7 @@ impl Image { let height = tag_reader.require_tag(Tag::ImageLength)?.into_u32()?; if width == 0 || height == 0 { return Err(TiffError::FormatError( - TiffFormatError::InconsistentSizesEncountered, + TiffFormatError::InvalidDimensions(width, height), )); } diff --git a/src/encoder/mod.rs b/src/encoder/mod.rs index 3547596..924ed8c 100644 --- a/src/encoder/mod.rs +++ b/src/encoder/mod.rs @@ -352,7 +352,7 @@ impl<'a, W: 'a + Write + Seek, T: ColorType, K: TiffKind, D: Compression> ) -> TiffResult { if width == 0 || height == 0 { return Err(TiffError::FormatError( - TiffFormatError::InconsistentSizesEncountered, + TiffFormatError::InvalidDimensions(width, height), )); } 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) From 907bbee5768c6e5b927719539f9f59cde61ffdb0 Mon Sep 17 00:00:00 2001 From: Jonathan Behrens Date: Mon, 5 Sep 2022 12:54:21 -0700 Subject: [PATCH 4/4] cargo fmt --- src/decoder/image.rs | 6 +++--- src/encoder/mod.rs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/decoder/image.rs b/src/decoder/image.rs index 7adffc7..6a4fc65 100644 --- a/src/decoder/image.rs +++ b/src/decoder/image.rs @@ -98,9 +98,9 @@ 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), - )); + return Err(TiffError::FormatError(TiffFormatError::InvalidDimensions( + width, height, + ))); } let photometric_interpretation = tag_reader diff --git a/src/encoder/mod.rs b/src/encoder/mod.rs index 924ed8c..6e39c93 100644 --- a/src/encoder/mod.rs +++ b/src/encoder/mod.rs @@ -351,9 +351,9 @@ impl<'a, W: 'a + Write + Seek, T: ColorType, K: TiffKind, D: Compression> compression: D, ) -> TiffResult { if width == 0 || height == 0 { - return Err(TiffError::FormatError( - TiffFormatError::InvalidDimensions(width, height), - )); + return Err(TiffError::FormatError(TiffFormatError::InvalidDimensions( + width, height, + ))); } let row_samples = u64::from(width) * u64::try_from(::BITS_PER_SAMPLE.len())?;