diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e5e1f5..0342b07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ - Optimized cache a little bit which saves ~200 bytes binary size in example - Add heap-backed cache types when the `alloc` feature is active. This gets rid of some (const) generics and allows you to create a cache with dynamic length. +- Added checks to avoid trying to write or read zero bytes of data to/from the flash This release is 'disk'-compatible with 6.0 diff --git a/src/item.rs b/src/item.rs index 3553cc4..fc3ce77 100644 --- a/src/item.rs +++ b/src/item.rs @@ -141,14 +141,16 @@ impl ItemHeader { let mut retry = false; loop { - flash - .read(data_address, &mut data_buffer[..read_len]) - .await - .map_err(|e| Error::Storage { - value: e, - #[cfg(feature = "_test")] - backtrace: std::backtrace::Backtrace::capture(), - })?; + if read_len != 0 { + flash + .read(data_address, &mut data_buffer[..read_len]) + .await + .map_err(|e| Error::Storage { + value: e, + #[cfg(feature = "_test")] + backtrace: std::backtrace::Backtrace::capture(), + })?; + } let data = &data_buffer[..self.length as usize]; let data_crc = adapted_crc32(data); @@ -290,14 +292,16 @@ impl<'d> Item<'d> { }; let data_address = ItemHeader::data_address::(address); - flash - .write(data_address, data_block) - .await - .map_err(|e| Error::Storage { - value: e, - #[cfg(feature = "_test")] - backtrace: std::backtrace::Backtrace::capture(), - })?; + if !data_block.is_empty() { + flash + .write(data_address, data_block) + .await + .map_err(|e| Error::Storage { + value: e, + #[cfg(feature = "_test")] + backtrace: std::backtrace::Backtrace::capture(), + })?; + } if !data_left.is_empty() { let mut buffer = AlignedBuf([0; MAX_WORD_SIZE]); diff --git a/src/mock_flash.rs b/src/mock_flash.rs index cb224fc..abc7f02 100644 --- a/src/mock_flash.rs +++ b/src/mock_flash.rs @@ -86,7 +86,7 @@ impl fn validate_operation(offset: u32, length: usize) -> Result, MockFlashError> { let offset = offset as usize; - if (offset % Self::READ_SIZE) != 0 { + if (offset % Self::READ_SIZE) != 0 || length == 0 || length % BYTES_PER_WORD != 0 { Err(MockFlashError::NotAligned) } else if offset > Self::CAPACITY_BYTES || offset + length > Self::CAPACITY_BYTES { Err(MockFlashError::OutOfBounds)