[clang] f7fec8c - [Clang] Prevent null pointer dereference in template deduction guide creation (#97097)

via cfe-commits cfe-commits at lists.llvm.org
Sat Jun 29 11:32:43 PDT 2024


Author: smanna12
Date: 2024-06-29T13:32:39-05:00
New Revision: f7fec8c80a986e82ef174080a3e07703c7b2c3c6

URL: https://github.com/llvm/llvm-project/commit/f7fec8c80a986e82ef174080a3e07703c7b2c3c6
DIFF: https://github.com/llvm/llvm-project/commit/f7fec8c80a986e82ef174080a3e07703c7b2c3c6.diff

LOG: [Clang] Prevent null pointer dereference in template deduction guide creation (#97097)

This patch addresses static analyzer concerns where `TSI` could be
dereferenced after being assigned a null value from `SubstType` in
`ConvertConstructorToDeductionGuideTransform()`.

The fixes now check null value of `TSI` after the call to `SubstType`
and return `nullptr` to prevent potential null pointer dereferences when
calling getTypeLoc() or getType() and ensure safe execution.

Added: 
    

Modified: 
    clang/lib/Sema/SemaTemplate.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index e36ee2d5a46cf..9f4acbe5e6dd5 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -2513,6 +2513,9 @@ struct ConvertConstructorToDeductionGuideTransform {
       TSI = SemaRef.SubstType(TSI, OuterInstantiationArgs, Loc,
                               DeductionGuideName);
 
+    if (!TSI)
+      return nullptr;
+
     FunctionProtoTypeLoc FPTL =
         TSI->getTypeLoc().castAs<FunctionProtoTypeLoc>();
 
@@ -2523,6 +2526,9 @@ struct ConvertConstructorToDeductionGuideTransform {
       if (NestedPattern)
         TSI = SemaRef.SubstType(TSI, OuterInstantiationArgs, Loc,
                                 DeclarationName());
+      if (!TSI)
+        return nullptr;
+
       ParmVarDecl *NewParam =
           ParmVarDecl::Create(SemaRef.Context, DC, Loc, Loc, nullptr,
                               TSI->getType(), TSI, SC_None, nullptr);


        


More information about the cfe-commits mailing list