Skip to content

Commit a7677f7

Browse files
committed
reference NonNull::dangling
1 parent 0f572a9 commit a7677f7

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

library/alloc/src/boxed.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,12 @@
6262
//! T` obtained from [`Box::<T>::into_raw`] may be deallocated using the
6363
//! [`Global`] allocator with [`Layout::for_value(&*value)`].
6464
//!
65-
//! For zero-sized values, the `Box` pointer still has to be [valid] for reads and writes and
66-
//! sufficiently aligned. In particular, casting any aligned non-zero integer literal to a raw
67-
//! pointer produces a valid pointer, but a pointer pointing into previously allocated memory that
68-
//! since got freed is not valid.
65+
//! For zero-sized values, the `Box` pointer still has to be [valid] for reads
66+
//! and writes and sufficiently aligned. In particular, casting any aligned
67+
//! non-zero integer literal to a raw pointer produces a valid pointer, but a
68+
//! pointer pointing into previously allocated memory that since got freed is
69+
//! not valid. The recommended way to build a Box to a ZST if `Box::new` cannot
70+
//! be used is to use [`ptr::NonNull::dangling`].
6971
//!
7072
//! So long as `T: Sized`, a `Box<T>` is guaranteed to be represented
7173
//! as a single pointer and is also ABI-compatible with C pointers

library/core/src/ptr/mod.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
//! be *dereferenceable*: the memory range of the given size starting at the pointer must all be
2121
//! within the bounds of a single allocated object. Note that in Rust,
2222
//! every (stack-allocated) variable is considered a separate allocated object.
23-
//! * Even for operations of [size zero][zst], the pointer must not be "dangling" in the sense of
24-
//! pointing to deallocated memory. However, casting any non-zero integer literal to a pointer is
25-
//! valid for zero-sized accesses. This corresponds to writing your own allocator; allocating
26-
//! zero-sized objects is not very hard. In contrast, when you use the standard allocator, after
27-
//! memory got deallocated, even zero-sized accesses to that memory are invalid.
23+
//! * Even for operations of [size zero][zst], the pointer must not be pointing to deallocated
24+
//! memory, i.e., deallocation makes pointers invalid even for zero-sized operations. However,
25+
//! casting any non-zero integer *literal* to a pointer is valid for zero-sized accesses, even if
26+
//! some memory happens to exist at that address and gets deallocated. This corresponds to writing
27+
//! your own allocator: allocating zero-sized objects is not very hard. The canonical way to
28+
//! obtain a pointer that is valid for zero-sized accesses is [`NonNull::dangling`].
2829
//! * All accesses performed by functions in this module are *non-atomic* in the sense
2930
//! of [atomic operations] used to synchronize between threads. This means it is
3031
//! undefined behavior to perform two concurrent accesses to the same location from different

0 commit comments

Comments
 (0)