mirror of
https://github.com/trussed-dev/littlefs2.git
synced 2026-03-11 16:30:52 -07:00
aafa02e332
Previously, we reported the lookahead buffer size in bytes but littlefs2-sys expects the lookahead buffer size as a multiple of 8 bytes. This could lead to a buffer overflow causing filesystem corruption. This patch fixes the reported lookahead buffer size. Note that Storage::LOOKAHEAD_WORDS_SIZE allows users to set invalid values (as it is measured in 4 bytes, not in 8 bytes). Invalid values that were previously accepted because of the wrong buffer size calculation can now be rejected by littlefs2-sys. This is a combination of two previous patches: https://github.com/trussed-dev/littlefs2/pull/19 https://github.com/Nitrokey/littlefs2/pull/1 Fixes: https://github.com/trussed-dev/littlefs2/issues/16
66 lines
1.8 KiB
Rust
66 lines
1.8 KiB
Rust
use littlefs2::{
|
|
consts,
|
|
fs::{File, Filesystem},
|
|
io::{Error, Result},
|
|
ram_storage, driver,
|
|
};
|
|
|
|
ram_storage!(
|
|
name=OtherRamStorage,
|
|
backend=OtherRam,
|
|
trait=driver::Storage,
|
|
erase_value=0xff,
|
|
read_size=1,
|
|
write_size=32,
|
|
cache_size_ty=consts::U32,
|
|
block_size=256,
|
|
block_count=512,
|
|
lookaheadwords_size_ty=consts::U2,
|
|
filename_max_plus_one_ty=consts::U256,
|
|
path_max_plus_one_ty=consts::U256,
|
|
result=Result,
|
|
);
|
|
|
|
ram_storage!(
|
|
name=RamStorage,
|
|
backend=Ram,
|
|
trait=driver::Storage,
|
|
erase_value=0xff,
|
|
read_size=20*5,
|
|
write_size=20*7,
|
|
cache_size_ty=consts::U700,
|
|
block_size=20*35,
|
|
block_count=32,
|
|
lookaheadwords_size_ty=consts::U2,
|
|
filename_max_plus_one_ty=consts::U256,
|
|
path_max_plus_one_ty=consts::U256,
|
|
result=Result,
|
|
);
|
|
|
|
fn main() {
|
|
let mut other_ram = OtherRam::default();
|
|
let mut other_storage = OtherRamStorage::new(&mut other_ram);
|
|
let mut alloc = Filesystem::allocate();
|
|
assert!(Filesystem::format(&mut other_storage).is_ok());
|
|
let other_fs = Filesystem::mount(&mut alloc, &mut other_storage).unwrap();
|
|
|
|
let mut ram = Ram::default();
|
|
let mut storage = RamStorage::new(&mut ram);
|
|
let mut alloc = Filesystem::allocate();
|
|
Filesystem::format(&mut storage).unwrap();
|
|
let mut fs = Filesystem::mount(&mut alloc, &mut storage).unwrap();
|
|
|
|
let mut alloc = File::allocate();
|
|
// file does not exist yet, can't open for reading
|
|
assert_eq!(
|
|
File::open("/test_open.txt", &mut alloc, &mut fs, &mut storage)
|
|
.map(drop)
|
|
.unwrap_err(), // "real" contains_err is experimental
|
|
Error::NoSuchEntry
|
|
);
|
|
|
|
// won't work: "expected struct `tests::RamStorage`, found struct
|
|
// `tests::RamStorageNormal`.
|
|
fs.create_dir("/tmp", &mut other_storage).unwrap();
|
|
}
|