Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

panic safety fix: guard against double drop #10

Merged
merged 1 commit into from
Jan 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,26 +174,24 @@ macro_rules! impl_array {
where
F: FnMut(T) -> U,
{
fn map_array(mut values: [T; $size], mut f: F) -> Self {
fn map_array(values: [T; $size], mut f: F) -> Self {
use std::{
mem::forget,
mem::{ManuallyDrop, MaybeUninit},
ptr::{read, write},
};

// Use `ManuallyDrop<_>` to guard against panic safety issue.
// Upon panic in `f`, `values` isn't dropped
// and thus item copied by `read()` is dropped only once.
let mut values = ManuallyDrop::new(values);
unsafe {
// All elements of `result` is written.
// Each element of `values` read once and then forgotten.
// Hence safe in case `f` never panics.
// TODO: Make it panic-safe.
let mut result: ::std::mem::MaybeUninit<[U; $size]> =
::std::mem::MaybeUninit::zeroed();
let mut result: MaybeUninit<[U; $size]> = MaybeUninit::zeroed();
for i in 0..$size {
write(
result.as_mut_ptr().cast::<U>().add(i),
f(read(&mut values[i])),
);
}
forget(values);
result.assume_init()
}
}
Expand Down