Skip to content

Commit 8ad723e

Browse files
committed
Fix out of range panic for 248+ long file names when alloc is disabled
1 parent 50b8dec commit 8ad723e

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ New features:
2828
* Create directory entry with `VOLUME_ID` attribute when formatting if volume label was set in `FormatVolumeOptions`.
2929
* Fix creating directory entries when `lfn` feature is enabled and `alloc` feature is disabled
3030
* Fix `format_volume` function panicking in debug build for FAT12 volumes with size below 1 MB
31+
* Fix index out of range panic when reading 248+ characters long file names with `alloc` feature disabled
3132
* Remove `byteorder` dependency.
3233
* Bump up minimal Rust compiler version to 1.46.0.
3334
* Build the crate using the 2018 edition.

src/dir.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -732,13 +732,17 @@ pub(crate) struct LfnBuffer {
732732
ucs2_units: Vec<u16>,
733733
}
734734

735+
const MAX_LONG_NAME_LEN: usize = 255;
736+
737+
const MAX_LONG_DIR_ENTRIES: usize = (MAX_LONG_NAME_LEN + LFN_PART_LEN - 1) / LFN_PART_LEN;
738+
735739
#[cfg(not(feature = "alloc"))]
736-
const MAX_LFN_LEN: usize = 256;
740+
const LONG_NAME_BUFFER_LEN: usize = MAX_LONG_DIR_ENTRIES * LFN_PART_LEN;
737741

738742
#[cfg(all(feature = "lfn", not(feature = "alloc")))]
739743
#[derive(Clone)]
740744
pub(crate) struct LfnBuffer {
741-
ucs2_units: [u16; MAX_LFN_LEN],
745+
ucs2_units: [u16; LONG_NAME_BUFFER_LEN],
742746
len: usize,
743747
}
744748

@@ -777,14 +781,14 @@ impl LfnBuffer {
777781
impl LfnBuffer {
778782
fn new() -> Self {
779783
Self {
780-
ucs2_units: [0_u16; MAX_LFN_LEN],
784+
ucs2_units: [0_u16; LONG_NAME_BUFFER_LEN],
781785
len: 0,
782786
}
783787
}
784788

785789
fn from_ucs2_units<I: Iterator<Item = u16>>(usc2_units: I) -> Self {
786790
let mut lfn = Self {
787-
ucs2_units: [0_u16; MAX_LFN_LEN],
791+
ucs2_units: [0_u16; LONG_NAME_BUFFER_LEN],
788792
len: 0,
789793
};
790794
for (i, usc2_unit) in usc2_units.enumerate() {
@@ -795,7 +799,7 @@ impl LfnBuffer {
795799
}
796800

797801
fn clear(&mut self) {
798-
self.ucs2_units = [0_u16; MAX_LFN_LEN];
802+
self.ucs2_units = [0_u16; LONG_NAME_BUFFER_LEN];
799803
self.len = 0;
800804
}
801805

@@ -875,7 +879,7 @@ impl LongNameBuilder {
875879
fn process(&mut self, data: &DirLfnEntryData) {
876880
let is_last = (data.order() & LFN_ENTRY_LAST_FLAG) != 0;
877881
let index = data.order() & 0x1F;
878-
if index == 0 {
882+
if index == 0 || usize::from(index) > MAX_LONG_DIR_ENTRIES {
879883
// Corrupted entry
880884
warn!("currupted lfn entry! {:x}", data.order());
881885
self.clear();

src/dir_entry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#[cfg(all(not(feature = "std"), feature = "alloc"))]
22
use alloc::string::String;
3+
use bitflags::bitflags;
34
use core::char;
45
use core::convert::TryInto;
56
use core::fmt;
67
#[cfg(not(feature = "unicode"))]
78
use core::iter;
89
use core::str;
9-
use bitflags::bitflags;
1010

1111
#[cfg(feature = "lfn")]
1212
use crate::dir::LfnBuffer;

0 commit comments

Comments
 (0)