@@ -559,27 +559,24 @@ impl<T> [T] {
559
559
// Use the llvm.bswap intrinsic to reverse u8s in a usize
560
560
let chunk = mem:: size_of :: < usize > ( ) ;
561
561
while i + chunk - 1 < ln / 2 {
562
- // SAFETY: An unaligned usize can be read from `i` if `i + 1 < ln`
563
- // (and obviously `i < ln`), because each element is 1 byte and
564
- // we're reading `chunk`.
562
+ // SAFETY:
565
563
//
566
- // Since we checked for the `x86` and `x86_64` target before
567
- // getting here so `chunk` is at most 8 bytes.
568
- //
569
- // `i + chunk - 1 < ln / 2` # while condition
570
- // `i + 8 - 1 < ln / 2`
571
- // `i + 7 < ln / 2`
572
- // so obviously `i + 1 < ln / 2`
573
- //
574
- // Since it's less than the length divided by 2, then it must be
575
- // in bounds.
576
- //
577
- // This also means that the condition `0 < i + chunk <= ln` is
578
- // always respected, ensuring the `pb` pointer can be used
579
- // safely.
580
- //
581
- // Note: when updating this comment, update the others in the
582
- // function too.
564
+ // - Note that `chunk` is either 4 or 8 due to the cfg check
565
+ // above. So `chunk - 1` is positive.
566
+ // - Indexing with index `i` is fine as the loop check guarantees
567
+ // `i + chunk - 1 < ln / 2`
568
+ // <=> `i < ln / 2 - (chunk - 1) < ln / 2 < ln`.
569
+ // - Indexing with index `ln - i - chunk = ln - (i + chunk)` is fine:
570
+ // - `i + chunk > 0` is trivially true.
571
+ // - The loop check guarantees:
572
+ // `i + chunk - 1 < ln / 2`
573
+ // <=> `i + chunk ≤ ln / 2 ≤ ln`, thus subtraction does not underflow.
574
+ // - The `read_unaligned` and `write_unaligned` calls are fine:
575
+ // - `pa` points to index `i` where `i < ln / 2 - (chunk - 1)`
576
+ // (see above) and `pb` points to index `ln - i - chunk`, so
577
+ // both are at least `chunk`
578
+ // many bytes away from the end of `self`.
579
+ // - Any initialized memory is valid `usize`.
583
580
unsafe {
584
581
let pa: * mut T = self . get_unchecked_mut ( i) ;
585
582
let pb: * mut T = self . get_unchecked_mut ( ln - i - chunk) ;
@@ -610,9 +607,6 @@ impl<T> [T] {
610
607
// This also means that the condition `0 < i + chunk <= ln` is
611
608
// always respected, ensuring the `pb` pointer can be used
612
609
// safely.
613
- //
614
- // Note: when updating this comment, update the others in the
615
- // function too.
616
610
unsafe {
617
611
let pa: * mut T = self . get_unchecked_mut ( i) ;
618
612
let pb: * mut T = self . get_unchecked_mut ( ln - i - chunk) ;
0 commit comments