From ef3294d1fe195525ccc8249a392f3c2c28dfa812 Mon Sep 17 00:00:00 2001 From: Jonathan Behrens Date: Sun, 4 Sep 2022 12:33:10 -0700 Subject: [PATCH] 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);