Skip to content

Commit 5a93033

Browse files
authored
[scudo] Minor refactor on element address validation (NFC) (#119436)
1 parent e7e42ef commit 5a93033

File tree

1 file changed

+11
-5
lines changed
  • compiler-rt/lib/scudo/standalone

1 file changed

+11
-5
lines changed

compiler-rt/lib/scudo/standalone/list.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ template <class T> class LinkOp<T, /*LinkWithPtr=*/false> {
7474
if (Next == nullptr) {
7575
X->Next = getEndOfListVal();
7676
} else {
77-
DCHECK_LE(static_cast<LinkTy>(Next - Base), Size);
77+
assertElementInRange(Next);
7878
X->Next = static_cast<LinkTy>(Next - Base);
7979
}
8080
}
@@ -88,16 +88,22 @@ template <class T> class LinkOp<T, /*LinkWithPtr=*/false> {
8888
}
8989
// Set `X->Prev` to `Prev`.
9090
void setPrev(T *X, T *Prev) const {
91-
DCHECK_LT(reinterpret_cast<uptr>(Prev),
92-
reinterpret_cast<uptr>(Base + Size));
93-
if (Prev == nullptr)
91+
if (Prev == nullptr) {
9492
X->Prev = getEndOfListVal();
95-
else
93+
} else {
94+
assertElementInRange(Prev);
9695
X->Prev = static_cast<LinkTy>(Prev - Base);
96+
}
9797
}
9898

9999
LinkTy getEndOfListVal() const { return T::EndOfListVal; }
100100

101+
private:
102+
void assertElementInRange(T *X) const {
103+
DCHECK_GE(reinterpret_cast<uptr>(X), reinterpret_cast<uptr>(Base));
104+
DCHECK_LE(static_cast<LinkTy>(X - Base), Size);
105+
}
106+
101107
protected:
102108
T *Base = nullptr;
103109
LinkTy Size = 0;

0 commit comments

Comments
 (0)