Skip to content

Commit 818a05d

Browse files
authored
Rollup merge of rust-lang#53784 - tbu-:pr_doc_slice_isize_max, r=RalfJung
Document that slices cannot be larger than `isize::MAX` bytes Fixes rust-lang#53676.
2 parents f55129d + e370b1c commit 818a05d

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/libcore/slice/mod.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use cmp::Ordering::{self, Less, Equal, Greater};
3434
use cmp;
3535
use fmt;
3636
use intrinsics::assume;
37+
use isize;
3738
use iter::*;
3839
use ops::{FnMut, Try, self};
3940
use option::Option;
@@ -4080,6 +4081,9 @@ unsafe impl<'a, T> TrustedRandomAccess for ChunksExactMut<'a, T> {
40804081
/// them from other data. You can obtain a pointer that is usable as `data`
40814082
/// for zero-length slices using [`NonNull::dangling()`].
40824083
///
4084+
/// The total size of the slice must be no larger than `isize::MAX` **bytes**
4085+
/// in memory. See the safety documentation of [`pointer::offset`].
4086+
///
40834087
/// # Caveat
40844088
///
40854089
/// The lifetime for the returned slice is inferred from its usage. To
@@ -4101,10 +4105,13 @@ unsafe impl<'a, T> TrustedRandomAccess for ChunksExactMut<'a, T> {
41014105
/// ```
41024106
///
41034107
/// [`NonNull::dangling()`]: ../../std/ptr/struct.NonNull.html#method.dangling
4108+
/// [`pointer::offset`]: ../../std/primitive.pointer.html#method.offset
41044109
#[inline]
41054110
#[stable(feature = "rust1", since = "1.0.0")]
41064111
pub unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
41074112
debug_assert!(data as usize % mem::align_of::<T>() == 0, "attempt to create unaligned slice");
4113+
debug_assert!(mem::size_of::<T>().saturating_mul(len) <= isize::MAX as usize,
4114+
"attempt to create slice covering half the address space");
41084115
Repr { raw: FatPtr { data, len } }.rust
41094116
}
41104117

@@ -4114,15 +4121,19 @@ pub unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
41144121
/// This function is unsafe for the same reasons as [`from_raw_parts`], as well
41154122
/// as not being able to provide a non-aliasing guarantee of the returned
41164123
/// mutable slice. `data` must be non-null and aligned even for zero-length
4117-
/// slices as with [`from_raw_parts`]. See the documentation of
4118-
/// [`from_raw_parts`] for more details.
4124+
/// slices as with [`from_raw_parts`]. The total size of the slice must be no
4125+
/// larger than `isize::MAX` **bytes** in memory.
4126+
///
4127+
/// See the documentation of [`from_raw_parts`] for more details.
41194128
///
41204129
/// [`from_raw_parts`]: ../../std/slice/fn.from_raw_parts.html
41214130
#[inline]
41224131
#[stable(feature = "rust1", since = "1.0.0")]
41234132
pub unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T] {
41244133
debug_assert!(data as usize % mem::align_of::<T>() == 0, "attempt to create unaligned slice");
4125-
Repr { raw: FatPtr { data, len} }.rust_mut
4134+
debug_assert!(mem::size_of::<T>().saturating_mul(len) <= isize::MAX as usize,
4135+
"attempt to create slice covering half the address space");
4136+
Repr { raw: FatPtr { data, len } }.rust_mut
41264137
}
41274138

41284139
/// Converts a reference to T into a slice of length 1 (without copying).

0 commit comments

Comments
 (0)