[clang] [Clang] Fix potential null pointer dereference in retain cycle detection (PR #95192)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jun 11 19:57:17 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: None (smanna12)
<details>
<summary>Changes</summary>
This patch resolves a static analyzer bug where `S.getCurMethodDecl()` could return `nullptr` when calling `getSelfDecl(()` and was being dereferenced without a null check. The fix introduces a check for a non-null return value before accessing `getSelfDecl()` to ensure safe dereferencing.
This change prevents undefined behavior in scenarios where the current method declaration is not available, thus enhancing the robustness of the retain cycle detection logic.
---
Full diff: https://github.com/llvm/llvm-project/pull/95192.diff
1 Files Affected:
- (modified) clang/lib/Sema/SemaObjC.cpp (+9-5)
``````````diff
diff --git a/clang/lib/Sema/SemaObjC.cpp b/clang/lib/Sema/SemaObjC.cpp
index d396258cfc7d1..69c78f034bd43 100644
--- a/clang/lib/Sema/SemaObjC.cpp
+++ b/clang/lib/Sema/SemaObjC.cpp
@@ -848,12 +848,16 @@ static bool findRetainCycleOwner(Sema &S, Expr *e, RetainCycleOwner &owner) {
owner.Indirect = true;
if (pre->isSuperReceiver()) {
- owner.Variable = S.getCurMethodDecl()->getSelfDecl();
- if (!owner.Variable)
+ if (const auto *CurMethodDecl = S.getCurMethodDecl()) {
+ owner.Variable = CurMethodDecl()->getSelfDecl();
+ if (!owner.Variable)
+ return false;
+ owner.Loc = pre->getLocation();
+ owner.Range = pre->getSourceRange();
+ return true;
+ } else {
return false;
- owner.Loc = pre->getLocation();
- owner.Range = pre->getSourceRange();
- return true;
+ }
}
e = const_cast<Expr *>(
cast<OpaqueValueExpr>(pre->getBase())->getSourceExpr());
``````````
</details>
https://github.com/llvm/llvm-project/pull/95192
More information about the cfe-commits
mailing list