diff --git a/src/lib.rs b/src/lib.rs index 97899e1..8722fad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -251,7 +251,7 @@ impl Ext4 { /// Read bytes into `dst`, starting at `start_byte`. fn read_bytes( &self, - start_byte: u64, + mut start_byte: u64, mut dst: &mut [u8], ) -> Result<(), Ext4Error> { // The first 1024 bytes are reserved for non-filesystem @@ -268,7 +268,7 @@ impl Ext4 { let block_index = self.0.journal.map_block_index(block_index); let offset_within_block = start_byte % block_size.to_u64(); - let read_len = { + let read_len: usize = { // OK to unwrap: `offset_within_block` is always less // than the block size, and the block size always fits // in a `u32`. We assume a `usize` is always at least as @@ -280,14 +280,15 @@ impl Ext4 { let partial_dst = &mut dst[..read_len]; - let start_byte = + let partial_start_byte = block_index * block_size.to_u64() + offset_within_block; self.0 .reader .borrow_mut() - .read(start_byte, partial_dst) + .read(partial_start_byte, partial_dst) .map_err(Ext4Error::Io)?; + start_byte += util::u64_from_usize(read_len); dst = &mut dst[read_len..]; } diff --git a/src/util.rs b/src/util.rs index d92fdb6..8fa503c 100644 --- a/src/util.rs +++ b/src/util.rs @@ -28,6 +28,26 @@ pub(crate) const fn usize_from_u32(val: u32) -> usize { } } +/// Convert a `usize` to a `u64`. +/// +/// TODO, but on platforms +/// supported by this crate, this conversion is infallible. +/// +/// # Panics +/// +/// Panics if `val` does not fit in this platform's `u64`. +#[inline] +#[must_use] +pub(crate) const fn u64_from_usize(val: usize) -> u64 { + assert!(size_of::() >= size_of::()); + + // Cannot use `usize::try_from` in a `const fn`. + #[expect(clippy::as_conversions)] + { + val as u64 + } +} + /// Create a `u64` from two `u32` values. #[inline] #[must_use]