diff --git a/compiler-rt/lib/scudo/standalone/list.h b/compiler-rt/lib/scudo/standalone/list.h index e7c69e5bb88d..e0b82788251b 100644 --- a/compiler-rt/lib/scudo/standalone/list.h +++ b/compiler-rt/lib/scudo/standalone/list.h @@ -74,7 +74,7 @@ public: if (Next == nullptr) { X->Next = getEndOfListVal(); } else { - DCHECK_LE(static_cast(Next - Base), Size); + assertElementInRange(Next); X->Next = static_cast(Next - Base); } } @@ -88,16 +88,22 @@ public: } // Set `X->Prev` to `Prev`. void setPrev(T *X, T *Prev) const { - DCHECK_LT(reinterpret_cast(Prev), - reinterpret_cast(Base + Size)); - if (Prev == nullptr) + if (Prev == nullptr) { X->Prev = getEndOfListVal(); - else + } else { + assertElementInRange(Prev); X->Prev = static_cast(Prev - Base); + } } LinkTy getEndOfListVal() const { return T::EndOfListVal; } +private: + void assertElementInRange(T *X) const { + DCHECK_GE(reinterpret_cast(X), reinterpret_cast(Base)); + DCHECK_LE(static_cast(X - Base), Size); + } + protected: T *Base = nullptr; LinkTy Size = 0;