From d0603fdafa61d1cc8c774f5845035d661093c7e9 Mon Sep 17 00:00:00 2001 From: John Bobbo Date: Sat, 15 Apr 2023 22:14:46 -0700 Subject: [PATCH 1/2] Use a `saturating_mul` instead of a `checked_mul` and `unwrap` in `core::intrinsics`. --- library/core/src/intrinsics.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/intrinsics.rs b/library/core/src/intrinsics.rs index a7c100e1b23ed..44ed9f76c481a 100644 --- a/library/core/src/intrinsics.rs +++ b/library/core/src/intrinsics.rs @@ -2519,7 +2519,7 @@ pub(crate) fn is_valid_allocation_size(len: usize) -> bool { pub(crate) fn is_nonoverlapping(src: *const T, dst: *const T, count: usize) -> bool { let src_usize = src.addr(); let dst_usize = dst.addr(); - let size = mem::size_of::().checked_mul(count).unwrap(); + let size = mem::size_of::().saturating_mul(count); let diff = if src_usize > dst_usize { src_usize - dst_usize } else { dst_usize - src_usize }; // If the absolute distance between the ptrs is at least as big as the size of the buffer, // they do not overlap. From 3dba5872a3b7e40e9c03aa89643266973c58fe1c Mon Sep 17 00:00:00 2001 From: John Bobbo Date: Sun, 16 Apr 2023 07:29:14 -0700 Subject: [PATCH 2/2] Add a message indicating overflow in `core::intrinsics::is_nonoverlapping`. --- library/core/src/intrinsics.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/library/core/src/intrinsics.rs b/library/core/src/intrinsics.rs index 44ed9f76c481a..ba03da411e34e 100644 --- a/library/core/src/intrinsics.rs +++ b/library/core/src/intrinsics.rs @@ -2519,7 +2519,9 @@ pub(crate) fn is_valid_allocation_size(len: usize) -> bool { pub(crate) fn is_nonoverlapping(src: *const T, dst: *const T, count: usize) -> bool { let src_usize = src.addr(); let dst_usize = dst.addr(); - let size = mem::size_of::().saturating_mul(count); + let size = mem::size_of::() + .checked_mul(count) + .expect("is_nonoverlapping: `size_of::() * count` overflows a usize"); let diff = if src_usize > dst_usize { src_usize - dst_usize } else { dst_usize - src_usize }; // If the absolute distance between the ptrs is at least as big as the size of the buffer, // they do not overlap.