Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7dc00b5

Browse files
committedJun 27, 2018
Remove use of uninitialized() in Weak::new()
This avoids the question of whether `uninitialized::<T>()` is necessarily UB when `T = !`
1 parent 142c98d commit 7dc00b5

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed
 

‎src/liballoc/arc.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use core::borrow;
2323
use core::fmt;
2424
use core::cmp::Ordering;
2525
use core::intrinsics::abort;
26-
use core::mem::{self, align_of_val, size_of_val, uninitialized};
26+
use core::mem::{self, align_of_val, size_of_val};
2727
use core::ops::Deref;
2828
use core::ops::CoerceUnsized;
2929
use core::ptr::{self, NonNull};
@@ -1028,13 +1028,13 @@ impl<T> Weak<T> {
10281028
#[stable(feature = "downgraded_weak", since = "1.10.0")]
10291029
pub fn new() -> Weak<T> {
10301030
unsafe {
1031-
Weak {
1032-
ptr: Box::into_raw_non_null(box ArcInner {
1033-
strong: atomic::AtomicUsize::new(0),
1034-
weak: atomic::AtomicUsize::new(1),
1035-
data: uninitialized(),
1036-
}),
1037-
}
1031+
let layout = Layout::new::<ArcInner<T>>();
1032+
let mut ptr = Global.alloc(layout)
1033+
.unwrap_or_else(|_| handle_alloc_error(layout))
1034+
.cast::<ArcInner<T>>();
1035+
ptr::write(&mut ptr.as_mut().strong, atomic::AtomicUsize::new(0));
1036+
ptr::write(&mut ptr.as_mut().weak, atomic::AtomicUsize::new(1));
1037+
Weak { ptr }
10381038
}
10391039
}
10401040
}

‎src/liballoc/rc.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ use core::hash::{Hash, Hasher};
253253
use core::intrinsics::abort;
254254
use core::marker;
255255
use core::marker::{Unsize, PhantomData};
256-
use core::mem::{self, align_of_val, forget, size_of_val, uninitialized};
256+
use core::mem::{self, align_of_val, forget, size_of_val};
257257
use core::ops::Deref;
258258
use core::ops::CoerceUnsized;
259259
use core::ptr::{self, NonNull};
@@ -1182,13 +1182,13 @@ impl<T> Weak<T> {
11821182
#[stable(feature = "downgraded_weak", since = "1.10.0")]
11831183
pub fn new() -> Weak<T> {
11841184
unsafe {
1185-
Weak {
1186-
ptr: Box::into_raw_non_null(box RcBox {
1187-
strong: Cell::new(0),
1188-
weak: Cell::new(1),
1189-
value: uninitialized(),
1190-
}),
1191-
}
1185+
let layout = Layout::new::<RcBox<T>>();
1186+
let mut ptr = Global.alloc(layout)
1187+
.unwrap_or_else(|_| handle_alloc_error(layout))
1188+
.cast::<RcBox<T>>();
1189+
ptr::write(&mut ptr.as_mut().strong, Cell::new(0));
1190+
ptr::write(&mut ptr.as_mut().weak, Cell::new(1));
1191+
Weak { ptr }
11921192
}
11931193
}
11941194
}

0 commit comments

Comments
 (0)
Please sign in to comment.