2
2
// Use of this source code is governed by a BSD-style license that can be
3
3
// found in the LICENSE file.
4
4
5
- #ifdef UNSAFE_BUFFERS_BUILD
6
- // TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
7
- #pragma allow_unsafe_buffers
8
- #endif
9
-
10
5
// IWYU pragma: private, include "base/memory/raw_ptr.h"
11
6
12
7
#ifndef PARTITION_ALLOC_POINTERS_RAW_PTR_H_
@@ -680,20 +675,23 @@ class PA_TRIVIAL_ABI PA_GSL_POINTER raw_ptr {
680
675
return static_cast <U*>(GetForExtraction ());
681
676
}
682
677
678
+ // PRECONDITIONS: `this` must not be at the end of the range.
683
679
PA_UNSAFE_BUFFER_USAGE PA_ALWAYS_INLINE constexpr raw_ptr& operator ++() {
684
680
static_assert (
685
681
raw_ptr_traits::IsPtrArithmeticAllowed (Traits),
686
682
" cannot increment raw_ptr unless AllowPtrArithmetic trait is present." );
687
- wrapped_ptr_ = Impl::Advance (wrapped_ptr_, 1 , true );
683
+ wrapped_ptr_ = PA_UNSAFE_TODO ( Impl::Advance (wrapped_ptr_, 1 , true ) );
688
684
return *this ;
689
685
}
686
+ // PRECONDITIONS: `this` must not be at the start of the range.
690
687
PA_UNSAFE_BUFFER_USAGE PA_ALWAYS_INLINE constexpr raw_ptr& operator --() {
691
688
static_assert (
692
689
raw_ptr_traits::IsPtrArithmeticAllowed (Traits),
693
690
" cannot decrement raw_ptr unless AllowPtrArithmetic trait is present." );
694
- wrapped_ptr_ = Impl::Retreat (wrapped_ptr_, 1 , true );
691
+ wrapped_ptr_ = PA_UNSAFE_TODO ( Impl::Retreat (wrapped_ptr_, 1 , true ) );
695
692
return *this ;
696
693
}
694
+ // PRECONDITIONS: `this` must not be at the end of the range.
697
695
PA_UNSAFE_BUFFER_USAGE PA_ALWAYS_INLINE constexpr raw_ptr operator ++(
698
696
int /* post_increment */ ) {
699
697
static_assert (
@@ -703,6 +701,7 @@ class PA_TRIVIAL_ABI PA_GSL_POINTER raw_ptr {
703
701
++(*this );
704
702
return result;
705
703
}
704
+ // PRECONDITIONS: `this` must not be at the start of the range.
706
705
PA_UNSAFE_BUFFER_USAGE PA_ALWAYS_INLINE constexpr raw_ptr operator --(
707
706
int /* post_decrement */ ) {
708
707
static_assert (
@@ -712,6 +711,7 @@ class PA_TRIVIAL_ABI PA_GSL_POINTER raw_ptr {
712
711
--(*this );
713
712
return result;
714
713
}
714
+ // PRECONDITIONS: `this` must be at least `delta_elems` before range end.
715
715
template <
716
716
typename Z,
717
717
typename = std::enable_if_t <partition_alloc::internal::is_offset_type<Z>>>
@@ -720,9 +720,11 @@ class PA_TRIVIAL_ABI PA_GSL_POINTER raw_ptr {
720
720
static_assert (
721
721
raw_ptr_traits::IsPtrArithmeticAllowed (Traits),
722
722
" cannot increment raw_ptr unless AllowPtrArithmetic trait is present." );
723
- wrapped_ptr_ = Impl::Advance (wrapped_ptr_, delta_elems, true );
723
+ wrapped_ptr_ =
724
+ PA_UNSAFE_TODO (Impl::Advance (wrapped_ptr_, delta_elems, true ));
724
725
return *this ;
725
726
}
727
+ // PRECONDITIONS: `this` must be at least `delta_elems` after range start.
726
728
template <
727
729
typename Z,
728
730
typename = std::enable_if_t <partition_alloc::internal::is_offset_type<Z>>>
@@ -731,10 +733,12 @@ class PA_TRIVIAL_ABI PA_GSL_POINTER raw_ptr {
731
733
static_assert (
732
734
raw_ptr_traits::IsPtrArithmeticAllowed (Traits),
733
735
" cannot decrement raw_ptr unless AllowPtrArithmetic trait is present." );
734
- wrapped_ptr_ = Impl::Retreat (wrapped_ptr_, delta_elems, true );
736
+ wrapped_ptr_ =
737
+ PA_UNSAFE_TODO (Impl::Retreat (wrapped_ptr_, delta_elems, true ));
735
738
return *this ;
736
739
}
737
740
741
+ // PRECONDITIONS: `delta_elems` must be an index inside the range.
738
742
template <typename Z,
739
743
typename U = T,
740
744
typename = std::enable_if_t <
@@ -748,7 +752,7 @@ class PA_TRIVIAL_ABI PA_GSL_POINTER raw_ptr {
748
752
// Call SafelyUnwrapPtrForDereference() to simulate what GetForDereference()
749
753
// does, but without creating a temporary.
750
754
return *Impl::SafelyUnwrapPtrForDereference (
751
- Impl::Advance (wrapped_ptr_, delta_elems, false ));
755
+ PA_UNSAFE_TODO ( Impl::Advance (wrapped_ptr_, delta_elems, false ) ));
752
756
}
753
757
754
758
// Do not disable operator+() and operator-().
@@ -764,6 +768,8 @@ class PA_TRIVIAL_ABI PA_GSL_POINTER raw_ptr {
764
768
// operators for Z=uint64_t on 32-bit systems. The compiler instead would
765
769
// generate code that converts `raw_ptr<T>` to `T*` and adds uint64_t to that,
766
770
// bypassing the OOB protection entirely.
771
+ //
772
+ // PRECONDITIONS: `this` must be at least `delta_elems` before range end.
767
773
template <typename Z>
768
774
PA_UNSAFE_BUFFER_USAGE PA_ALWAYS_INLINE friend constexpr raw_ptr operator +(
769
775
const raw_ptr& p,
@@ -773,15 +779,18 @@ class PA_TRIVIAL_ABI PA_GSL_POINTER raw_ptr {
773
779
static_assert (
774
780
raw_ptr_traits::IsPtrArithmeticAllowed (Traits),
775
781
" cannot add to raw_ptr unless AllowPtrArithmetic trait is present." );
776
- raw_ptr result = Impl::Advance (p.wrapped_ptr_ , delta_elems, false );
782
+ raw_ptr result =
783
+ PA_UNSAFE_TODO (Impl::Advance (p.wrapped_ptr_ , delta_elems, false ));
777
784
return result;
778
785
}
786
+ // PRECONDITIONS: `this` must be at least `delta_elems` before range end.
779
787
template <typename Z>
780
788
PA_UNSAFE_BUFFER_USAGE PA_ALWAYS_INLINE friend constexpr raw_ptr operator +(
781
789
Z delta_elems,
782
790
const raw_ptr& p) {
783
791
return p + delta_elems;
784
792
}
793
+ // PRECONDITIONS: `this` must be at least `delta_elems` after range start.
785
794
template <typename Z>
786
795
PA_UNSAFE_BUFFER_USAGE PA_ALWAYS_INLINE friend constexpr raw_ptr operator -(
787
796
const raw_ptr& p,
@@ -791,7 +800,8 @@ class PA_TRIVIAL_ABI PA_GSL_POINTER raw_ptr {
791
800
static_assert (raw_ptr_traits::IsPtrArithmeticAllowed (Traits),
792
801
" cannot subtract from raw_ptr unless AllowPtrArithmetic "
793
802
" trait is present." );
794
- raw_ptr result = Impl::Retreat (p.wrapped_ptr_ , delta_elems, false );
803
+ raw_ptr result =
804
+ PA_UNSAFE_TODO (Impl::Retreat (p.wrapped_ptr_ , delta_elems, false ));
795
805
return result;
796
806
}
797
807
0 commit comments