Skip to content

Commit bec5d37

Browse files
committed
debug_assert a few more raw pointer methods
1 parent 8040bc9 commit bec5d37

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/libcore/intrinsics.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1423,13 +1423,14 @@ pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool {
14231423
}
14241424

14251425
/// Checks whether the regions of memory starting at `src` and `dst` of size
1426-
/// `count * size_of::<T>()` overlap.
1427-
fn overlaps<T>(src: *const T, dst: *const T, count: usize) -> bool {
1426+
/// `count * size_of::<T>()` do *not* overlap.
1427+
pub(crate) fn is_nonoverlapping<T>(src: *const T, dst: *const T, count: usize) -> bool {
14281428
let src_usize = src as usize;
14291429
let dst_usize = dst as usize;
14301430
let size = mem::size_of::<T>().checked_mul(count).unwrap();
14311431
let diff = if src_usize > dst_usize { src_usize - dst_usize } else { dst_usize - src_usize };
1432-
size > diff
1432+
let overlaps = size > diff;
1433+
!overlaps
14331434
}
14341435

14351436
/// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
@@ -1524,7 +1525,7 @@ pub unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize) {
15241525

15251526
debug_assert!(is_aligned_and_not_null(src), "attempt to copy from unaligned or null pointer");
15261527
debug_assert!(is_aligned_and_not_null(dst), "attempt to copy to unaligned or null pointer");
1527-
debug_assert!(!overlaps(src, dst, count), "attempt to copy to overlapping memory");
1528+
debug_assert!(is_nonoverlapping(src, dst, count), "attempt to copy to overlapping memory");
15281529
copy_nonoverlapping(src, dst, count)
15291530
}
15301531

src/libcore/ptr/mod.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
use crate::cmp::Ordering;
7373
use crate::fmt;
7474
use crate::hash;
75-
use crate::intrinsics;
75+
use crate::intrinsics::{self, is_aligned_and_not_null, is_nonoverlapping};
7676
use crate::mem::{self, MaybeUninit};
7777

7878
#[stable(feature = "rust1", since = "1.0.0")]
@@ -389,6 +389,10 @@ pub unsafe fn swap<T>(x: *mut T, y: *mut T) {
389389
#[inline]
390390
#[stable(feature = "swap_nonoverlapping", since = "1.27.0")]
391391
pub unsafe fn swap_nonoverlapping<T>(x: *mut T, y: *mut T, count: usize) {
392+
debug_assert!(is_aligned_and_not_null(x), "attempt to swap unaligned or null pointer");
393+
debug_assert!(is_aligned_and_not_null(y), "attempt to swap unaligned or null pointer");
394+
debug_assert!(is_nonoverlapping(x, y, count), "attempt to swap overlapping memory");
395+
392396
let x = x as *mut u8;
393397
let y = y as *mut u8;
394398
let len = mem::size_of::<T>() * count;
@@ -612,6 +616,7 @@ pub unsafe fn replace<T>(dst: *mut T, mut src: T) -> T {
612616
#[inline]
613617
#[stable(feature = "rust1", since = "1.0.0")]
614618
pub unsafe fn read<T>(src: *const T) -> T {
619+
// `copy_nonoverlapping` takes care of debug_assert.
615620
let mut tmp = MaybeUninit::<T>::uninit();
616621
copy_nonoverlapping(src, tmp.as_mut_ptr(), 1);
617622
tmp.assume_init()
@@ -703,6 +708,7 @@ pub unsafe fn read<T>(src: *const T) -> T {
703708
#[inline]
704709
#[stable(feature = "ptr_unaligned", since = "1.17.0")]
705710
pub unsafe fn read_unaligned<T>(src: *const T) -> T {
711+
// `copy_nonoverlapping` takes care of debug_assert.
706712
let mut tmp = MaybeUninit::<T>::uninit();
707713
copy_nonoverlapping(src as *const u8, tmp.as_mut_ptr() as *mut u8, mem::size_of::<T>());
708714
tmp.assume_init()
@@ -795,6 +801,7 @@ pub unsafe fn read_unaligned<T>(src: *const T) -> T {
795801
#[inline]
796802
#[stable(feature = "rust1", since = "1.0.0")]
797803
pub unsafe fn write<T>(dst: *mut T, src: T) {
804+
debug_assert!(is_aligned_and_not_null(dst), "attempt to write to unaligned or null pointer");
798805
intrinsics::move_val_init(&mut *dst, src)
799806
}
800807

@@ -887,6 +894,7 @@ pub unsafe fn write<T>(dst: *mut T, src: T) {
887894
#[inline]
888895
#[stable(feature = "ptr_unaligned", since = "1.17.0")]
889896
pub unsafe fn write_unaligned<T>(dst: *mut T, src: T) {
897+
// `copy_nonoverlapping` takes care of debug_assert.
890898
copy_nonoverlapping(&src as *const T as *const u8, dst as *mut u8, mem::size_of::<T>());
891899
mem::forget(src);
892900
}
@@ -956,6 +964,7 @@ pub unsafe fn write_unaligned<T>(dst: *mut T, src: T) {
956964
#[inline]
957965
#[stable(feature = "volatile", since = "1.9.0")]
958966
pub unsafe fn read_volatile<T>(src: *const T) -> T {
967+
debug_assert!(is_aligned_and_not_null(src), "attempt to read from unaligned or null pointer");
959968
intrinsics::volatile_load(src)
960969
}
961970

@@ -1024,6 +1033,7 @@ pub unsafe fn read_volatile<T>(src: *const T) -> T {
10241033
#[inline]
10251034
#[stable(feature = "volatile", since = "1.9.0")]
10261035
pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
1036+
debug_assert!(is_aligned_and_not_null(dst), "attempt to write to unaligned or null pointer");
10271037
intrinsics::volatile_store(dst, src);
10281038
}
10291039

0 commit comments

Comments
 (0)