@@ -561,11 +561,15 @@ impl<T> [T] {
561
561
while i + chunk - 1 < ln / 2 {
562
562
// SAFETY: An unaligned usize can be read from `i` if `i + 1 < ln`
563
563
// (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.
565
568
//
566
569
// `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`
569
573
//
570
574
// Since it's less than the length divided by 2, then it must be
571
575
// in bounds.
@@ -656,8 +660,9 @@ impl<T> [T] {
656
660
let ptr = self . as_ptr ( ) ;
657
661
// SAFETY: There are several things here:
658
662
//
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` .
661
666
//
662
667
// Adding `self.len()` to the starting pointer gives a pointer
663
668
// at the end of `self`. `end` will never be dereferenced, only checked
@@ -699,8 +704,9 @@ impl<T> [T] {
699
704
let ptr = self . as_mut_ptr ( ) ;
700
705
// SAFETY: There are several things here:
701
706
//
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` .
704
710
//
705
711
// Adding `self.len()` to the starting pointer gives a pointer
706
712
// at the end of `self`. `end` will never be dereferenced, only checked
@@ -2296,8 +2302,8 @@ impl<T> [T] {
2296
2302
let k = self . len ( ) - mid;
2297
2303
let p = self . as_mut_ptr ( ) ;
2298
2304
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` .
2301
2307
unsafe {
2302
2308
rotate:: ptr_rotate ( mid, p. add ( mid) , k) ;
2303
2309
}
@@ -2339,8 +2345,8 @@ impl<T> [T] {
2339
2345
let mid = self . len ( ) - k;
2340
2346
let p = self . as_mut_ptr ( ) ;
2341
2347
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` .
2344
2350
unsafe {
2345
2351
rotate:: ptr_rotate ( mid, p. add ( mid) , k) ;
2346
2352
}
0 commit comments