Skip to content

Commit 2d87542

Browse files
mount_or_else: ensure that allocation has not been overwritten in the else clause
1 parent f2fc946 commit 2d87542

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

src/fs.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,13 @@ impl<'a, Storage: driver::Storage> Filesystem<'a, Storage> {
11041104
Ok(fs)
11051105
}
11061106

1107+
fn set_alloc_config(alloc: &mut Allocation<Storage>, storage: &mut Storage) {
1108+
alloc.config.context = storage as *mut _ as *mut c_void;
1109+
alloc.config.read_buffer = alloc.cache.read.get() as *mut c_void;
1110+
alloc.config.prog_buffer = alloc.cache.write.get() as *mut c_void;
1111+
alloc.config.lookahead_buffer = alloc.cache.lookahead.get() as *mut c_void;
1112+
}
1113+
11071114
/// Mount the filesystem or, if that fails, call `f` with the mount error and the storage and then try again.
11081115
pub fn mount_or_else<F>(
11091116
alloc: &'a mut Allocation<Storage>,
@@ -1113,9 +1120,11 @@ impl<'a, Storage: driver::Storage> Filesystem<'a, Storage> {
11131120
where
11141121
F: FnOnce(Error, &mut Storage, &mut Allocation<Storage>) -> Result<()>,
11151122
{
1116-
let fs = Self::new(alloc, storage);
1123+
let mut fs = Self::new(alloc, storage);
11171124
if let Err(err) = fs.raw_mount() {
1118-
f(err, fs.storage, &mut fs.alloc.borrow_mut())?;
1125+
let alloc = fs.alloc.get_mut();
1126+
f(err, fs.storage, alloc)?;
1127+
Self::set_alloc_config(alloc, fs.storage);
11191128
fs.raw_mount()?;
11201129
}
11211130
Ok(fs)
@@ -1130,12 +1139,7 @@ impl<'a, Storage: driver::Storage> Filesystem<'a, Storage> {
11301139

11311140
// Not public, user should use `mount`, possibly after `format`
11321141
fn new(alloc: &'a mut Allocation<Storage>, storage: &'a mut Storage) -> Self {
1133-
alloc.config.context = storage as *mut _ as *mut c_void;
1134-
1135-
alloc.config.read_buffer = alloc.cache.read.get() as *mut c_void;
1136-
alloc.config.prog_buffer = alloc.cache.write.get() as *mut c_void;
1137-
alloc.config.lookahead_buffer = alloc.cache.lookahead.get() as *mut c_void;
1138-
1142+
Self::set_alloc_config(alloc, storage);
11391143
Filesystem {
11401144
alloc: RefCell::new(alloc),
11411145
storage,

src/tests.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use core::convert::TryInto;
22
use generic_array::typenum::consts;
33

44
use crate::{
5-
fs::{Attribute, File, Filesystem},
5+
fs::{Allocation, Attribute, File, Filesystem},
66
io::{Error, OpenSeekFrom, Read, Result, SeekFrom},
77
path, BACKEND_VERSION, DISK_VERSION,
88
};
@@ -521,6 +521,19 @@ fn test_iter_dirs() {
521521
.unwrap();
522522
}
523523

524+
#[test]
525+
fn test_mount_or_else_clobber_alloc() {
526+
let mut backend = Ram::default();
527+
let mut storage = RamStorage::new(&mut backend);
528+
let alloc = &mut Allocation::new();
529+
Filesystem::mount_or_else(alloc, &mut storage, |_, storage, alloc| {
530+
*alloc = Allocation::new();
531+
Filesystem::format(storage).unwrap();
532+
Ok(())
533+
})
534+
.unwrap();
535+
}
536+
524537
// // These are some tests that ensure our type constructions
525538
// // actually do what we intend them to do.
526539
// // Since dev-features cannot be optional, trybuild is not `no_std`,

0 commit comments

Comments
 (0)