diff --git a/crates/clawhdf5-format/src/object_header.rs b/crates/clawhdf5-format/src/object_header.rs index d0f36db..1fa4b41 100644 --- a/crates/clawhdf5-format/src/object_header.rs +++ b/crates/clawhdf5-format/src/object_header.rs @@ -385,13 +385,13 @@ impl ObjectHeader { } let chunk0_msg_start = pos; - let chunk0_msg_end = abs(pos) - .checked_add(chunk0_size) - .ok_or(FormatError::UnexpectedEof { + let Some(chunk0_abs_end) = abs(pos).checked_add(chunk0_size) else { + return Err(FormatError::UnexpectedEof { expected: usize::MAX, available: file_len, - })? - - base; + }); + }; + let chunk0_msg_end = chunk0_abs_end - base; // The whole first chunk, prefix to checksum, in one read (its // bounds check is the one on the checksum's 4 bytes). @@ -1364,7 +1364,11 @@ mod tests { let want = ObjectHeader::parse(&f, at, 8, 8); let storage = CountingStorage::new(f.clone()); let got = ObjectHeader::parse_in(&storage, at as u64, 8, 8); - assert_eq!(format!("{got:?}"), format!("{want:?}"), "at {at}, cut {cut}"); + assert_eq!( + format!("{got:?}"), + format!("{want:?}"), + "at {at}, cut {cut}" + ); } } } diff --git a/crates/clawhdf5-format/src/storage.rs b/crates/clawhdf5-format/src/storage.rs index 76f1436..fb19f32 100644 --- a/crates/clawhdf5-format/src/storage.rs +++ b/crates/clawhdf5-format/src/storage.rs @@ -215,7 +215,11 @@ pub fn read_exact_at( /// storage. For structures whose size is only known once their prefix has /// been parsed and whose parsers bound-check what they are given. #[inline] -pub fn read_upto(file: &dyn Storage, offset: u64, max: usize) -> Result, FormatError> { +pub fn read_upto( + file: &dyn Storage, + offset: u64, + max: usize, +) -> Result, FormatError> { let avail = file.len().saturating_sub(offset); let len = usize::try_from(avail).map_or(max, |a| a.min(max)); let bytes = file.read_at(offset, len)?; diff --git a/crates/clawhdf5-format/src/superblock_ext.rs b/crates/clawhdf5-format/src/superblock_ext.rs index 40efd5c..1a684f6 100644 --- a/crates/clawhdf5-format/src/superblock_ext.rs +++ b/crates/clawhdf5-format/src/superblock_ext.rs @@ -520,7 +520,10 @@ fn image_block_in( } /// Where the image block is, checked against a file of `file_len` bytes. -fn image_block_range(file_len: u64, location: CacheImageLocation) -> Result<(u64, usize), FormatError> { +fn image_block_range( + file_len: u64, + location: CacheImageLocation, +) -> Result<(u64, usize), FormatError> { let bad = FormatError::InvalidCacheImage; let start = usize::try_from(location.address).map_err(|_| bad("address out of range"))?; let len = usize::try_from(location.length).map_err(|_| bad("length out of range"))?; @@ -883,9 +886,15 @@ mod tests { read_superblock_extension_in(&storage, &sb), read_superblock_extension(&f, &sb) ); - assert_eq!(cache_image_state_in(&storage, &sb), cache_image_state(&f, &sb)); + assert_eq!( + cache_image_state_in(&storage, &sb), + cache_image_state(&f, &sb) + ); if let Ok(CacheImageState::Loaded(image)) = cache_image_state(&f, &sb) { - assert_eq!(&*image.block_in(&storage).unwrap(), image.block(&f).unwrap()); + assert_eq!( + &*image.block_in(&storage).unwrap(), + image.block(&f).unwrap() + ); } } }