Skip to content

Commit 5d9086f

Browse files
committed
Fix overflow in from_raw_parts size check
1 parent 1aac00f commit 5d9086f

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/libcore/slice/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3881,7 +3881,8 @@ unsafe impl<'a, T> TrustedRandomAccess for ExactChunksMut<'a, T> {
38813881
#[stable(feature = "rust1", since = "1.0.0")]
38823882
pub unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
38833883
debug_assert!(data as usize % mem::align_of::<T>() == 0, "attempt to create unaligned slice");
3884-
debug_assert!(len * mem::size_of::<T>() < isize::MAX as usize,
3884+
let size = mem::size_of::<T>();
3885+
debug_assert!(size == 0 || len < (isize::MAX as usize + size - 1) / size,
38853886
"attempt to create slice covering half the address space");
38863887
Repr { raw: FatPtr { data, len } }.rust
38873888
}
@@ -3904,9 +3905,10 @@ pub unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
39043905
#[stable(feature = "rust1", since = "1.0.0")]
39053906
pub unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T] {
39063907
debug_assert!(data as usize % mem::align_of::<T>() == 0, "attempt to create unaligned slice");
3907-
debug_assert!(len * mem::size_of::<T>() < isize::MAX as usize,
3908+
let size = mem::size_of::<T>();
3909+
debug_assert!(size == 0 || len < (isize::MAX as usize + size - 1) / size,
39083910
"attempt to create slice covering half the address space");
3909-
Repr { raw: FatPtr { data, len} }.rust_mut
3911+
Repr { raw: FatPtr { data, len } }.rust_mut
39103912
}
39113913

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

0 commit comments

Comments
 (0)