[clang] [Clang] Prevent null pointer dereference in designated initializer check (PR #97220)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jul 16 11:29:23 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/4] [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/4] 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);
>From ca4a91f41048e30dc373a40a019216813df0e7c0 Mon Sep 17 00:00:00 2001
From: "Manna, Soumi" <soumi.manna at intel.com>
Date: Sun, 30 Jun 2024 10:00:26 -0700
Subject: [PATCH 3/4] Fix Clang format errors
---
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 f943b5f3b5339..80e35f79a172d 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 ExprResult((Expr*)nullptr);
+ return ExprResult((Expr *)nullptr);
bool isDesignated =
CurMD->isDesignatedInitializerForTheInterface(&InitMethod);
assert(isDesignated && InitMethod);
>From 716fcd79b56ac6ccc8c209b43a652bc985ce8e7a Mon Sep 17 00:00:00 2001
From: "Manna, Soumi" <soumi.manna at intel.com>
Date: Tue, 16 Jul 2024 11:28:50 -0700
Subject: [PATCH 4/4] Address review comments
---
clang/lib/Sema/SemaExprObjC.cpp | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/clang/lib/Sema/SemaExprObjC.cpp b/clang/lib/Sema/SemaExprObjC.cpp
index 80e35f79a172d..2751c7cec2842 100644
--- a/clang/lib/Sema/SemaExprObjC.cpp
+++ b/clang/lib/Sema/SemaExprObjC.cpp
@@ -3207,8 +3207,7 @@ ExprResult SemaObjC::BuildInstanceMessage(
if (!isDesignatedInitChain) {
const ObjCMethodDecl *InitMethod = nullptr;
auto *CurMD = SemaRef.getCurMethodDecl();
- if (!CurMD)
- return ExprResult((Expr *)nullptr);
+ assert(CurMD && "Current method declaration should not be null");
bool isDesignated =
CurMD->isDesignatedInitializerForTheInterface(&InitMethod);
assert(isDesignated && InitMethod);
More information about the cfe-commits
mailing list