Skip to content

Commit 9b6cfc4

Browse files
authored
Rollup merge of rust-lang#112704 - RalfJung:dont-wrap-slices, r=ChrisDenton
slice::from_raw_parts: mention no-wrap-around condition Cc rust-lang#83996. This probably needs to be mentioned in more places, so I am not closing that issue, but this here should help at least.
2 parents 25eab45 + 18b8646 commit 9b6cfc4

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

library/core/src/slice/raw.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ use crate::ptr;
3232
/// * The memory referenced by the returned slice must not be mutated for the duration
3333
/// of lifetime `'a`, except inside an `UnsafeCell`.
3434
///
35-
/// * The total size `len * mem::size_of::<T>()` of the slice must be no larger than `isize::MAX`.
35+
/// * The total size `len * mem::size_of::<T>()` of the slice must be no larger than `isize::MAX`,
36+
/// and adding that size to `data` must not "wrap around" the address space.
3637
/// See the safety documentation of [`pointer::offset`].
3738
///
3839
/// # Caveat
@@ -125,7 +126,8 @@ pub const unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T]
125126
/// (not derived from the return value) for the duration of lifetime `'a`.
126127
/// Both read and write accesses are forbidden.
127128
///
128-
/// * The total size `len * mem::size_of::<T>()` of the slice must be no larger than `isize::MAX`.
129+
/// * The total size `len * mem::size_of::<T>()` of the slice must be no larger than `isize::MAX`,
130+
/// and adding that size to `data` must not "wrap around" the address space.
129131
/// See the safety documentation of [`pointer::offset`].
130132
///
131133
/// [valid]: ptr#safety
@@ -179,15 +181,16 @@ pub const fn from_mut<T>(s: &mut T) -> &mut [T] {
179181
/// the last element, such that the offset from the end to the start pointer is
180182
/// the length of the slice.
181183
///
182-
/// * The range must contain `N` consecutive properly initialized values of type `T`:
184+
/// * The entire memory range of this slice must be contained within a single allocated object!
185+
/// Slices can never span across multiple allocated objects.
183186
///
184-
/// * The entire memory range of this slice must be contained within a single allocated object!
185-
/// Slices can never span across multiple allocated objects.
187+
/// * The range must contain `N` consecutive properly initialized values of type `T`.
186188
///
187189
/// * The memory referenced by the returned slice must not be mutated for the duration
188190
/// of lifetime `'a`, except inside an `UnsafeCell`.
189191
///
190-
/// * The total length of the range must be no larger than `isize::MAX`.
192+
/// * The total length of the range must be no larger than `isize::MAX`,
193+
/// and adding that size to `data` must not "wrap around" the address space.
191194
/// See the safety documentation of [`pointer::offset`].
192195
///
193196
/// Note that a range created from [`slice::as_ptr_range`] fulfills these requirements.
@@ -247,16 +250,17 @@ pub const unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] {
247250
/// the last element, such that the offset from the end to the start pointer is
248251
/// the length of the slice.
249252
///
250-
/// * The range must contain `N` consecutive properly initialized values of type `T`:
253+
/// * The entire memory range of this slice must be contained within a single allocated object!
254+
/// Slices can never span across multiple allocated objects.
251255
///
252-
/// * The entire memory range of this slice must be contained within a single allocated object!
253-
/// Slices can never span across multiple allocated objects.
256+
/// * The range must contain `N` consecutive properly initialized values of type `T`.
254257
///
255258
/// * The memory referenced by the returned slice must not be accessed through any other pointer
256259
/// (not derived from the return value) for the duration of lifetime `'a`.
257260
/// Both read and write accesses are forbidden.
258261
///
259-
/// * The total length of the range must be no larger than `isize::MAX`.
262+
/// * The total length of the range must be no larger than `isize::MAX`,
263+
/// and adding that size to `data` must not "wrap around" the address space.
260264
/// See the safety documentation of [`pointer::offset`].
261265
///
262266
/// Note that a range created from [`slice::as_mut_ptr_range`] fulfills these requirements.

0 commit comments

Comments
 (0)