From 45cd3c3b638567cf1eecfcbf1e193d5282b50030 Mon Sep 17 00:00:00 2001 From: "A. Molzer" <5550310+197g@users.noreply.github.com> Date: Thu, 5 Feb 2026 02:19:15 +0100 Subject: [PATCH] Improve unexpected tile count error Do we even want to error early here? This isn't exactly fatal to reading the image it is a only nuisance. It becomes crucial only when we are at the point of reading all the tiles themselves. --- src/decoder/image.rs | 40 +++++++++++++++++++++++++++++++--------- src/error.rs | 3 +++ 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/decoder/image.rs b/src/decoder/image.rs index 5330328..4af80e9 100644 --- a/src/decoder/image.rs +++ b/src/decoder/image.rs @@ -330,15 +330,28 @@ impl Image { strip_decoder = Some(StripDecodeState { rows_per_strip }); tile_attributes = None; - if chunk_offsets.len() != chunk_bytes.len() - || rows_per_strip == 0 - || u32::try_from(chunk_offsets.len())? - != (height.saturating_sub(1) / rows_per_strip + 1) * planes as u32 - { + if chunk_offsets.len() != chunk_bytes.len() { return Err(TiffError::FormatError( TiffFormatError::InconsistentSizesEncountered, )); } + + if rows_per_strip == 0 { + return Err(TiffError::FormatError( + TiffFormatError::InconsistentSizesEncountered, + )); + } + + if u32::try_from(chunk_offsets.len())? + != height.div_ceil(rows_per_strip) * planes as u32 + { + return Err(TiffError::FormatError( + TiffFormatError::IncorrectChunkCount { + expected: height.div_ceil(rows_per_strip) * planes as u32, + found: chunk_offsets.len() as u32, + }, + )); + } } (false, false, true, true) => { chunk_type = ChunkType::Tile; @@ -371,14 +384,23 @@ impl Image { .into_u64_vec()?; let tile = tile_attributes.as_ref().unwrap(); - if chunk_offsets.len() != chunk_bytes.len() - || chunk_offsets.len() - != tile.tiles_down() * tile.tiles_across() * planes as usize - { + + if chunk_offsets.len() != chunk_bytes.len() { return Err(TiffError::FormatError( TiffFormatError::InconsistentSizesEncountered, )); } + + if chunk_offsets.len() != tile.tiles_down() * tile.tiles_across() * planes as usize + { + return Err(TiffError::FormatError( + TiffFormatError::IncorrectChunkCount { + expected: (tile.tiles_down() * tile.tiles_across() * planes as usize) + as u32, + found: chunk_offsets.len() as u32, + }, + )); + } } (_, _, _, _) => { return Err(TiffError::FormatError( diff --git a/src/error.rs b/src/error.rs index 9311915..95c0a65 100644 --- a/src/error.rs +++ b/src/error.rs @@ -70,6 +70,9 @@ quick_error! { InconsistentSizesEncountered { display("inconsistent sizes encountered") } + IncorrectChunkCount {expected: u32, found: u32} { + display("incorrect number of chunk offsets encoded for the tile count of the image, expected {expected} but found {found}") + } InvalidDimensions(width: u32, height: u32) { display("invalid dimensions: {width}x{height}") }