https://github.com/smanna12 created https://github.com/llvm/llvm-project/pull/95192
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. >From 6852bd6773c13dd9e573df460e74e2b7306c63f0 Mon Sep 17 00:00:00 2001 From: "Manna, Soumi" <soumi.ma...@intel.com> Date: Tue, 11 Jun 2024 19:52:03 -0700 Subject: [PATCH] [Clang] Fix potential null pointer dereference in retain cycle detection 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. --- clang/lib/Sema/SemaObjC.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) 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()); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits