Skip to content

Commit 987218d

Browse files
authored
Merge pull request #1418 from clubby789/fix-ub
Prevent UB with `assume_init` on uninit bytes
2 parents e95ec05 + 0c5246c commit 987218d

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/ebr/deferred.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use core::ptr;
1010
const DATA_WORDS: usize = 3;
1111

1212
/// Some space to keep a `FnOnce()` object on the stack.
13-
type Data = [usize; DATA_WORDS];
13+
type Data = MaybeUninit<[usize; DATA_WORDS]>;
1414

1515
/// A `FnOnce()` that is stored inline if small, or otherwise boxed on the heap.
1616
///
@@ -50,22 +50,22 @@ impl Deferred {
5050
if size <= mem::size_of::<Data>()
5151
&& align <= mem::align_of::<Data>()
5252
{
53-
let mut data = MaybeUninit::<Data>::uninit();
53+
let mut data = Data::uninit();
5454
ptr::write(data.as_mut_ptr() as *mut F, f);
5555

5656
Deferred {
5757
call: call_raw::<F>,
58-
data: data.assume_init(),
58+
data,
5959
_marker: PhantomData,
6060
}
6161
} else {
6262
let b: Box<F> = Box::new(f);
63-
let mut data = MaybeUninit::<Data>::uninit();
63+
let mut data = Data::uninit();
6464
ptr::write(data.as_mut_ptr() as *mut Box<F>, b);
6565

6666
Deferred {
6767
call: call_raw_box::<F>,
68-
data: data.assume_init(),
68+
data,
6969
_marker: PhantomData,
7070
}
7171
}
@@ -78,7 +78,7 @@ impl Deferred {
7878
let call = self.call;
7979
#[allow(trivial_casts)]
8080
unsafe {
81-
call(&mut self.data as *mut Data as *mut u8)
81+
call(self.data.as_mut_ptr().cast())
8282
};
8383
}
8484
}

0 commit comments

Comments
 (0)