Skip to content

Commit 5a0de2f

Browse files
committed
Improve safety comments for usize, fix some other unclear parts
1 parent 3a709fe commit 5a0de2f

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

library/core/src/slice/mod.rs

+17-11
Original file line numberDiff line numberDiff line change
@@ -561,11 +561,15 @@ impl<T> [T] {
561561
while i + chunk - 1 < ln / 2 {
562562
// SAFETY: An unaligned usize can be read from `i` if `i + 1 < ln`
563563
// (and obviously `i < ln`), because each element is 1 byte and
564-
// we're reading 2.
564+
// we're reading `chunk`.
565+
//
566+
// Since we checked for the `x86` and `x86_64` target before
567+
// getting here so `chunk` is at most 8 bytes.
565568
//
566569
// `i + chunk - 1 < ln / 2` # while condition
567-
// `i + 2 - 1 < ln / 2`
568-
// `i + 1 < ln / 2`
570+
// `i + 8 - 1 < ln / 2`
571+
// `i + 7 < ln / 2`
572+
// so obviously `i + 1 < ln / 2`
569573
//
570574
// Since it's less than the length divided by 2, then it must be
571575
// in bounds.
@@ -656,8 +660,9 @@ impl<T> [T] {
656660
let ptr = self.as_ptr();
657661
// SAFETY: There are several things here:
658662
//
659-
// `ptr` has been checked for nullity before being passed to `NonNull` via
660-
// `new_unchecked`.
663+
// `ptr` has been obtained by `self.as_ptr()` where `self` is a valid
664+
// reference thus it is non-NUL and safe to use and pass to
665+
// `NonNull::new_unchecked` .
661666
//
662667
// Adding `self.len()` to the starting pointer gives a pointer
663668
// at the end of `self`. `end` will never be dereferenced, only checked
@@ -699,8 +704,9 @@ impl<T> [T] {
699704
let ptr = self.as_mut_ptr();
700705
// SAFETY: There are several things here:
701706
//
702-
// `ptr` has been checked for nullity before being passed to `NonNull` via
703-
// `new_unchecked`.
707+
// `ptr` has been obtained by `self.as_ptr()` where `self` is a valid
708+
// reference thus it is non-NUL and safe to use and pass to
709+
// `NonNull::new_unchecked` .
704710
//
705711
// Adding `self.len()` to the starting pointer gives a pointer
706712
// at the end of `self`. `end` will never be dereferenced, only checked
@@ -2296,8 +2302,8 @@ impl<T> [T] {
22962302
let k = self.len() - mid;
22972303
let p = self.as_mut_ptr();
22982304

2299-
// SAFETY: `[mid; mid+k]` corresponds to the entire
2300-
// `self` slice, thus is valid for reads and writes.
2305+
// SAFETY: The range `[p.add(mid) - mid, p.add(mid) + k)` is trivially
2306+
// valid for reading and writing, as required by `ptr_rotate`.
23012307
unsafe {
23022308
rotate::ptr_rotate(mid, p.add(mid), k);
23032309
}
@@ -2339,8 +2345,8 @@ impl<T> [T] {
23392345
let mid = self.len() - k;
23402346
let p = self.as_mut_ptr();
23412347

2342-
// SAFETY: `[mid; mid+k]` corresponds to the entire
2343-
// `self` slice, thus is valid for reads and writes.
2348+
// SAFETY: The range `[p.add(mid) - mid, p.add(mid) + k)` is trivially
2349+
// valid for reading and writing, as required by `ptr_rotate`.
23442350
unsafe {
23452351
rotate::ptr_rotate(mid, p.add(mid), k);
23462352
}

0 commit comments

Comments
 (0)