[clang] [Clang] Fix potential null pointer dereferences in Sema::AddInitializerToDecl (PR #94368)

via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 28 14:18:46 PDT 2024


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

>From b6d45ded3d0d1ad6a50a1292d4f8275081089150 Mon Sep 17 00:00:00 2001
From: "Manna, Soumi" <soumi.manna at intel.com>
Date: Tue, 4 Jun 2024 08:33:51 -0700
Subject: [PATCH 1/5] [Clang] Fix potential null pointer dereferences in
 Sema::AddInitializerToDecl

This patch adds null check for 'Init' before dereferencing it to prevent
potential null pointer dereferences reported by static Analyzer tool in
the function.
---
 clang/lib/Sema/SemaDecl.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 34e46e12859bb..cd50df646b8b2 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -13728,7 +13728,7 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) {
     // paths through the function. This should be revisited if
     // -Wrepeated-use-of-weak is made flow-sensitive.
     if (FunctionScopeInfo *FSI = getCurFunction())
-      if ((VDecl->getType().getObjCLifetime() == Qualifiers::OCL_Strong ||
+      if (Init && (VDecl->getType().getObjCLifetime() == Qualifiers::OCL_Strong ||
            VDecl->getType().isNonWeakInMRRWithObjCWeak(Context)) &&
           !Diags.isIgnored(diag::warn_arc_repeated_use_of_weak,
                            Init->getBeginLoc()))

>From 14d874cbfa18b321c15b5bea7409efc5aa388da2 Mon Sep 17 00:00:00 2001
From: "Manna, Soumi" <soumi.manna at intel.com>
Date: Tue, 4 Jun 2024 08:44:23 -0700
Subject: [PATCH 2/5] Fix Clang format errors

---
 clang/lib/Sema/SemaDecl.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index cd50df646b8b2..474e393de669c 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -13728,7 +13728,8 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) {
     // paths through the function. This should be revisited if
     // -Wrepeated-use-of-weak is made flow-sensitive.
     if (FunctionScopeInfo *FSI = getCurFunction())
-      if (Init && (VDecl->getType().getObjCLifetime() == Qualifiers::OCL_Strong ||
+      if (Init &&
+          (VDecl->getType().getObjCLifetime() == Qualifiers::OCL_Strong ||
            VDecl->getType().isNonWeakInMRRWithObjCWeak(Context)) &&
           !Diags.isIgnored(diag::warn_arc_repeated_use_of_weak,
                            Init->getBeginLoc()))

>From fc124205388bd5c87aa9848d9b7ba51476c83d8b Mon Sep 17 00:00:00 2001
From: "Manna, Soumi" <soumi.manna at intel.com>
Date: Tue, 4 Jun 2024 09:31:09 -0700
Subject: [PATCH 3/5] Address review comments

---
 clang/lib/Sema/SemaDecl.cpp | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 474e393de669c..292236bf1a0ee 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -13681,6 +13681,8 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) {
     }
 
     Init = Result.getAs<Expr>();
+    assert (Init && "Init must not be null");
+
     IsParenListInit = !InitSeq.steps().empty() &&
                       InitSeq.step_begin()->Kind ==
                           InitializationSequence::SK_ParenthesizedListInit;
@@ -13728,8 +13730,7 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) {
     // paths through the function. This should be revisited if
     // -Wrepeated-use-of-weak is made flow-sensitive.
     if (FunctionScopeInfo *FSI = getCurFunction())
-      if (Init &&
-          (VDecl->getType().getObjCLifetime() == Qualifiers::OCL_Strong ||
+      if ((VDecl->getType().getObjCLifetime() == Qualifiers::OCL_Strong ||
            VDecl->getType().isNonWeakInMRRWithObjCWeak(Context)) &&
           !Diags.isIgnored(diag::warn_arc_repeated_use_of_weak,
                            Init->getBeginLoc()))

>From 5b2da669506c49f4bdb2d9a6c53758c0f8480b72 Mon Sep 17 00:00:00 2001
From: "Manna, Soumi" <soumi.manna at intel.com>
Date: Tue, 4 Jun 2024 09:36:23 -0700
Subject: [PATCH 4/5] Fix clang format errors

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

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 292236bf1a0ee..5439aedec0885 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -13681,7 +13681,7 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) {
     }
 
     Init = Result.getAs<Expr>();
-    assert (Init && "Init must not be null");
+    assert(Init && "Init must not be null");
 
     IsParenListInit = !InitSeq.steps().empty() &&
                       InitSeq.step_begin()->Kind ==

>From 400d6851767aa675fe34dc509256d37089d22ee9 Mon Sep 17 00:00:00 2001
From: "Manna, Soumi" <soumi.manna at intel.com>
Date: Fri, 28 Jun 2024 14:18:13 -0700
Subject: [PATCH 5/5] Fix lit failure

---
 clang/lib/Sema/SemaDecl.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 5439aedec0885..f13c9a3efc9e6 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -13687,8 +13687,7 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) {
                       InitSeq.step_begin()->Kind ==
                           InitializationSequence::SK_ParenthesizedListInit;
     QualType VDeclType = VDecl->getType();
-    if (Init && !Init->getType().isNull() &&
-        !Init->getType()->isDependentType() && !VDeclType->isDependentType() &&
+    if (!Init->getType()->isDependentType() && !VDeclType->isDependentType() &&
         Context.getAsIncompleteArrayType(VDeclType) &&
         Context.getAsIncompleteArrayType(Init->getType())) {
       // Bail out if it is not possible to deduce array size from the



More information about the cfe-commits mailing list