From e26a7027a2ae95fd9aad8f3f4399e8485a9c297f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Jacobs?= Date: Fri, 26 Jul 2024 17:34:55 +0200 Subject: [PATCH] Return an error when encountering corrupted lfn entry --- embedded-fatfs/src/dir.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/embedded-fatfs/src/dir.rs b/embedded-fatfs/src/dir.rs index 01a6ea5..9395a31 100644 --- a/embedded-fatfs/src/dir.rs +++ b/embedded-fatfs/src/dir.rs @@ -779,7 +779,7 @@ impl<'a, IO: ReadWriteSeek, TP: TimeProvider, OCC> DirIter<'a, IO, TP, OCC> { DirEntryData::Lfn(data) => { // Append to LFN buffer trace!("lfn entry"); - lfn_builder.process(&data); + lfn_builder.process::(&data)?; } } } @@ -1003,14 +1003,14 @@ impl LongNameBuilder { self.index == 0 } - fn process(&mut self, data: &DirLfnEntryData) { + fn process(&mut self, data: &DirLfnEntryData) -> Result<(), Error> { let is_last = (data.order() & LFN_ENTRY_LAST_FLAG) != 0; let index = data.order() & 0x1F; if index == 0 || usize::from(index) > MAX_LONG_DIR_ENTRIES { // Corrupted entry warn!("corrupted lfn entry! {:x}", data.order()); self.clear(); - return; + return Err(Error::CorruptedFileSystem); } if is_last { // last entry is actually first entry in stream @@ -1027,7 +1027,7 @@ impl LongNameBuilder { self.chksum ); self.clear(); - return; + return Err(Error::CorruptedFileSystem); } else { // Decrement LFN index only for non-last entries self.index -= 1; @@ -1035,6 +1035,7 @@ impl LongNameBuilder { let pos = LFN_PART_LEN * usize::from(index - 1); // copy name parts into LFN buffer data.copy_name_to_slice(&mut self.buf.ucs2_units[pos..pos + 13]); + Ok(()) } fn validate_chksum(&mut self, short_name: &[u8; SFN_SIZE]) { @@ -1061,7 +1062,9 @@ impl LongNameBuilder { fn clear(&mut self) {} fn into_vec(self) {} fn truncate(&mut self) {} - fn process(&mut self, _data: &DirLfnEntryData) {} + fn process(&mut self, _data: &DirLfnEntryData) -> Result<(), Error> { + Ok(()) + } fn validate_chksum(&mut self, _short_name: &[u8; SFN_SIZE]) {} }