Skip to content

Commit

Permalink
Return an error when encountering corrupted lfn entry
Browse files Browse the repository at this point in the history
  • Loading branch information
aurelj committed Sep 20, 2024
1 parent 8ca54b6 commit 636de0f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
13 changes: 8 additions & 5 deletions embedded-fatfs/src/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<IO>(&data)?;
}
}
}
Expand Down Expand Up @@ -1003,14 +1003,14 @@ impl LongNameBuilder {
self.index == 0
}

fn process(&mut self, data: &DirLfnEntryData) {
fn process<IO: ReadWriteSeek>(&mut self, data: &DirLfnEntryData) -> Result<(), Error<IO::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::CorruptedFileEntry);
}
if is_last {
// last entry is actually first entry in stream
Expand All @@ -1027,14 +1027,15 @@ impl LongNameBuilder {
self.chksum
);
self.clear();
return;
return Err(Error::CorruptedFileEntry);
} else {
// Decrement LFN index only for non-last entries
self.index -= 1;
}
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]) {
Expand All @@ -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<IO: ReadWriteSeek>(&mut self, _data: &DirLfnEntryData) -> Result<(), Error<IO::Error>> {
Ok(())
}
fn validate_chksum(&mut self, _short_name: &[u8; SFN_SIZE]) {}
}

Expand Down
3 changes: 3 additions & 0 deletions embedded-fatfs/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub enum Error<T> {
InvalidFileNameLength,
/// The provided file name contains an invalid character.
UnsupportedFileNameCharacter,
/// An entry in the file table is corrupted/invalid.
CorruptedFileEntry,
}

impl<T: Debug> IoError for Error<T> {
Expand Down Expand Up @@ -76,6 +78,7 @@ impl<T: core::fmt::Display> core::fmt::Display for Error<T> {
Error::NotFound => write!(f, "No such file or directory"),
Error::AlreadyExists => write!(f, "File or directory already exists"),
Error::CorruptedFileSystem => write!(f, "Corrupted file system"),
Error::CorruptedFileEntry => write!(f, "Corrupted file entry"),
}
}
}
Expand Down

0 comments on commit 636de0f

Please sign in to comment.