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 Jul 26, 2024
1 parent 1d5ffda commit e26a702
Showing 1 changed file with 8 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::CorruptedFileSystem);
}
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::CorruptedFileSystem);
} 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

0 comments on commit e26a702

Please sign in to comment.