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

via cfe-commits cfe-commits at lists.llvm.org
Sun Jun 30 09:47:48 PDT 2024


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

>From 63f45c952ff8ab7df261a150355a34267e4a645c Mon Sep 17 00:00:00 2001
From: "Manna, Soumi" <soumi.manna at intel.com>
Date: Sun, 30 Jun 2024 08:40:27 -0700
Subject: [PATCH 1/2] [Clang] Prevent null pointer dereference in designated
 initializer check

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().
---
 clang/lib/Sema/SemaExprObjC.cpp | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

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() ?

>From 44c309e87d2101b554321741b1c751247a5b6478 Mon Sep 17 00:00:00 2001
From: "Manna, Soumi" <soumi.manna at intel.com>
Date: Sun, 30 Jun 2024 09:47:03 -0700
Subject: [PATCH 2/2] Fix build failure

---
 clang/lib/Sema/SemaExprObjC.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaExprObjC.cpp b/clang/lib/Sema/SemaExprObjC.cpp
index 05aa30e16ed8e..f943b5f3b5339 100644
--- a/clang/lib/Sema/SemaExprObjC.cpp
+++ b/clang/lib/Sema/SemaExprObjC.cpp
@@ -3208,7 +3208,7 @@ ExprResult SemaObjC::BuildInstanceMessage(
       const ObjCMethodDecl *InitMethod = nullptr;
       auto *CurMD = SemaRef.getCurMethodDecl();
       if (!CurMD)
-        return nullptr;
+        return ExprResult((Expr*)nullptr);
       bool isDesignated =
           CurMD->isDesignatedInitializerForTheInterface(&InitMethod);
       assert(isDesignated && InitMethod);



More information about the cfe-commits mailing list