Skip to content

Commit f3f559b

Browse files
committed
Document explicitly that Weak::from_raw(ptr::null()) is ub
1 parent e173a8e commit f3f559b

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

library/alloc/src/rc.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -2776,8 +2776,16 @@ impl<T: ?Sized> Weak<T> {
27762776
///
27772777
/// # Safety
27782778
///
2779-
/// The pointer must have originated from the [`into_raw`] and must still own its potential
2780-
/// weak reference, and `ptr` must point to a block of memory allocated by the global allocator.
2779+
/// The pointer must have originated from the [`into_raw`] or [`into_raw_and_alloc`] functions
2780+
/// and must still own its potential weak reference, and `ptr` must point to a block of
2781+
/// memory allocated by `alloc`.
2782+
///
2783+
/// Note that `from_raw` expects values that actually originated from a call to one of these
2784+
/// functions and have not been used with `from_raw` yet, not what these functions can maybe
2785+
/// return, or are documented to potentially return.
2786+
/// For example, [`into_raw`] can return dangling pointers, but this doesn't allow you to create
2787+
/// a dangling pointer yourself and pass it to `from_raw`. Even if it has the same address
2788+
/// as a pointer created by [`into_raw`], use [`Weak::new`] instead.
27812789
///
27822790
/// It is allowed for the strong count to be 0 at the time of calling this. Nevertheless, this
27832791
/// takes ownership of one weak reference currently represented as a raw pointer (the weak
@@ -2806,6 +2814,7 @@ impl<T: ?Sized> Weak<T> {
28062814
/// ```
28072815
///
28082816
/// [`into_raw`]: Weak::into_raw
2817+
/// [`into_raw_and_alloc`]: Weak::into_raw_and_alloc
28092818
/// [`upgrade`]: Weak::upgrade
28102819
/// [`new`]: Weak::new
28112820
#[inline]
@@ -2942,8 +2951,16 @@ impl<T: ?Sized, A: Allocator> Weak<T, A> {
29422951
///
29432952
/// # Safety
29442953
///
2945-
/// The pointer must have originated from the [`into_raw`] and must still own its potential
2946-
/// weak reference, and `ptr` must point to a block of memory allocated by `alloc`.
2954+
/// The pointer must have originated from the [`into_raw`] or [`into_raw_and_alloc`] functions
2955+
/// and must still own its potential weak reference, and `ptr` must point to a block of
2956+
/// memory allocated by `alloc`.
2957+
///
2958+
/// Note that `from_raw` expects values that actually originated from a call to one of these
2959+
/// functions and have not been used with `from_raw` yet, not what these functions can maybe
2960+
/// return, or are documented to potentially return.
2961+
/// For example, [`into_raw`] can return dangling pointers, but this doesn't allow you to create
2962+
/// a dangling pointer yourself and pass it to `from_raw`. Even if it has the same address
2963+
/// as a pointer created by [`into_raw`], use [`Weak::new`] instead.
29472964
///
29482965
/// It is allowed for the strong count to be 0 at the time of calling this. Nevertheless, this
29492966
/// takes ownership of one weak reference currently represented as a raw pointer (the weak
@@ -2972,6 +2989,7 @@ impl<T: ?Sized, A: Allocator> Weak<T, A> {
29722989
/// ```
29732990
///
29742991
/// [`into_raw`]: Weak::into_raw
2992+
/// [`into_raw_and_alloc`]: Weak::into_raw_and_alloc
29752993
/// [`upgrade`]: Weak::upgrade
29762994
/// [`new`]: Weak::new
29772995
#[inline]

library/alloc/src/sync.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -2686,8 +2686,15 @@ impl<T: ?Sized, A: Allocator> Weak<T, A> {
26862686
///
26872687
/// # Safety
26882688
///
2689-
/// The pointer must have originated from the [`into_raw`] and must still own its potential
2690-
/// weak reference, and must point to a block of memory allocated by `alloc`.
2689+
/// The pointer must have originated from the [`into_raw`] function and must still own its
2690+
/// potential weak reference, and must point to a block of memory allocated by `alloc`.
2691+
///
2692+
/// Note that `from_raw_in` expects values that actually originated from a call to [`into_raw`]
2693+
/// and have not been used with `from_raw` yet, not what [`into_raw`] can maybe return,
2694+
/// or is documented to potentially return.
2695+
/// For example, [`into_raw`] can return dangling pointers, but this doesn't allow you to create
2696+
/// a dangling pointer yourself and pass it to `from_raw`. Even if it has the same address
2697+
/// as a pointer created by [`into_raw`], use [`Weak::new`] instead.
26912698
///
26922699
/// It is allowed for the strong count to be 0 at the time of calling this. Nevertheless, this
26932700
/// takes ownership of one weak reference currently represented as a raw pointer (the weak

0 commit comments

Comments
 (0)