Skip to content

Commit c555aab

Browse files
committed
clarify rules for ZST Boxes
1 parent 06a079c commit c555aab

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

library/alloc/src/boxed.rs

+9
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@
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 to a raw pointer
67+
//! produces a valid pointer, but a pointer pointing into previously allocated memory that since got
68+
//! freed is not valid.
69+
//!
6570
//! So long as `T: Sized`, a `Box<T>` is guaranteed to be represented
6671
//! as a single pointer and is also ABI-compatible with C pointers
6772
//! (i.e. the C type `T*`). This means that if you have extern "C"
@@ -125,6 +130,7 @@
125130
//! [`Global`]: crate::alloc::Global
126131
//! [`Layout`]: crate::alloc::Layout
127132
//! [`Layout::for_value(&*value)`]: crate::alloc::Layout::for_value
133+
//! [valid]: ptr#safety
128134
129135
#![stable(feature = "rust1", since = "1.0.0")]
130136

@@ -385,7 +391,10 @@ impl<T: ?Sized> Box<T> {
385391
/// memory problems. For example, a double-free may occur if the
386392
/// function is called twice on the same raw pointer.
387393
///
394+
/// The safety conditions are described in the [memory layout] section.
395+
///
388396
/// # Examples
397+
///
389398
/// Recreate a `Box` which was previously converted to a raw pointer
390399
/// using [`Box::into_raw`]:
391400
/// ```

library/core/src/ptr/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616
//! provided at this point are very minimal:
1717
//!
1818
//! * A [null] pointer is *never* valid, not even for accesses of [size zero][zst].
19-
//! * All pointers (except for the null pointer) are valid for all operations of
20-
//! [size zero][zst].
2119
//! * For a pointer to be valid, it is necessary, but not always sufficient, that the pointer
2220
//! be *dereferenceable*: the memory range of the given size starting at the pointer must all be
2321
//! within the bounds of a single allocated object. Note that in Rust,
2422
//! 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 to a pointer is valid
25+
//! for zero-sized accesses. This corresponds to writing your own allocator; allocating zero-sized
26+
//! objects is not very hard. In contrast, when you use the standard allocator, after memory got
27+
//! deallocated, even zero-sized accesses to that memory are invalid.
2528
//! * All accesses performed by functions in this module are *non-atomic* in the sense
2629
//! of [atomic operations] used to synchronize between threads. This means it is
2730
//! undefined behavior to perform two concurrent accesses to the same location from different

0 commit comments

Comments
 (0)