[clang] [Clang] Prevent null pointer dereference in designated initializer check (PR #97220)

via cfe-commits cfe-commits at lists.llvm.org
Sun Jun 30 08:45:01 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: None (smanna12)

<details>
<summary>Changes</summary>

This patch adds a null check for the current method declaration before attempting to determine if it is a designated initializer.

This prevents a potential null pointer dereference when `getCurMethodDecl()` returns nullptr, reported by static analyzer tool in clang::SemaObjC::BuildInstanceMessage().

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


1 Files Affected:

- (modified) clang/lib/Sema/SemaExprObjC.cpp (+4-2) 


``````````diff
diff --git a/clang/lib/Sema/SemaExprObjC.cpp b/clang/lib/Sema/SemaExprObjC.cpp
index 7ccecf055feed..05aa30e16ed8e 100644
--- a/clang/lib/Sema/SemaExprObjC.cpp
+++ b/clang/lib/Sema/SemaExprObjC.cpp
@@ -3206,9 +3206,11 @@ ExprResult SemaObjC::BuildInstanceMessage(
     }
     if (!isDesignatedInitChain) {
       const ObjCMethodDecl *InitMethod = nullptr;
+      auto *CurMD = SemaRef.getCurMethodDecl();
+      if (!CurMD)
+        return nullptr;
       bool isDesignated =
-          SemaRef.getCurMethodDecl()->isDesignatedInitializerForTheInterface(
-              &InitMethod);
+          CurMD->isDesignatedInitializerForTheInterface(&InitMethod);
       assert(isDesignated && InitMethod);
       (void)isDesignated;
       Diag(SelLoc, SuperLoc.isValid() ?

``````````

</details>


https://github.com/llvm/llvm-project/pull/97220


More information about the cfe-commits mailing list