Skip to content

[scudo] Minor refactor on element address validation (NFC) #119436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2024

Conversation

ChiaHungDuan
Copy link
Contributor

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Dec 10, 2024

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: None (ChiaHungDuan)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/119436.diff

1 Files Affected:

  • (modified) compiler-rt/lib/scudo/standalone/list.h (+11-5)
diff --git a/compiler-rt/lib/scudo/standalone/list.h b/compiler-rt/lib/scudo/standalone/list.h
index e7c69e5bb88d67..e0b82788251b7f 100644
--- a/compiler-rt/lib/scudo/standalone/list.h
+++ b/compiler-rt/lib/scudo/standalone/list.h
@@ -74,7 +74,7 @@ template <class T> class LinkOp<T, /*LinkWithPtr=*/false> {
     if (Next == nullptr) {
       X->Next = getEndOfListVal();
     } else {
-      DCHECK_LE(static_cast<LinkTy>(Next - Base), Size);
+      assertElementInRange(Next);
       X->Next = static_cast<LinkTy>(Next - Base);
     }
   }
@@ -88,16 +88,22 @@ template <class T> class LinkOp<T, /*LinkWithPtr=*/false> {
   }
   // Set `X->Prev` to `Prev`.
   void setPrev(T *X, T *Prev) const {
-    DCHECK_LT(reinterpret_cast<uptr>(Prev),
-              reinterpret_cast<uptr>(Base + Size));
-    if (Prev == nullptr)
+    if (Prev == nullptr) {
       X->Prev = getEndOfListVal();
-    else
+    } else {
+      assertElementInRange(Prev);
       X->Prev = static_cast<LinkTy>(Prev - Base);
+    }
   }
 
   LinkTy getEndOfListVal() const { return T::EndOfListVal; }
 
+private:
+  void assertElementInRange(T *X) const {
+    DCHECK_GE(reinterpret_cast<uptr>(X), reinterpret_cast<uptr>(Base));
+    DCHECK_LE(static_cast<LinkTy>(X - Base), Size);
+  }
+
 protected:
   T *Base = nullptr;
   LinkTy Size = 0;

Copy link
Contributor

@cferris1000 cferris1000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ChiaHungDuan ChiaHungDuan merged commit 5a93033 into llvm:main Dec 10, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants