Skip to content

Commit 3c22676

Browse files
Fix the Allocation lifetime in File::open
This also a breaking change because the `unsafe` `open` function did not take the correct lifetime. It think it's an acceptable breaking change because the previous behaviour was buggy and any code that depends on it likely has a use after free.
1 parent b3a7371 commit 3c22676

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/fs.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Experimental Filesystem version using closures.
22
33
use core::ffi::{c_int, c_void};
4+
use core::marker::PhantomData;
45
use core::ptr::addr_of;
56
use core::ptr::addr_of_mut;
67
use core::{
@@ -613,6 +614,7 @@ pub struct File<'a, 'b, S: driver::Storage> {
613614
// We must store a raw pointer here since the FFI retains a copy of a pointer
614615
// to the field alloc.state, so we cannot assert unique mutable access.
615616
alloc: RefCell<*mut FileAllocation<S>>,
617+
phantom: PhantomData<RefCell<&'b mut FileAllocation<S>>>,
616618
fs: &'b Filesystem<'a, S>,
617619
}
618620

@@ -803,7 +805,7 @@ impl OpenOptions {
803805
pub unsafe fn open<'a, 'b, S: driver::Storage>(
804806
&self,
805807
fs: &'b Filesystem<'a, S>,
806-
alloc: &mut FileAllocation<S>,
808+
alloc: &'b mut FileAllocation<S>,
807809
path: &Path,
808810
) -> Result<File<'a, 'b, S>> {
809811
alloc.config.buffer = alloc.cache.get() as *mut _;
@@ -820,6 +822,7 @@ impl OpenOptions {
820822

821823
let file = File {
822824
alloc: RefCell::new(alloc),
825+
phantom: PhantomData,
823826
fs,
824827
};
825828

0 commit comments

Comments
 (0)