[clang] 05d8ea7 - [Clang] Prevent null pointer dereferences in SVE tuple functions (#94267)

via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 1 08:51:32 PDT 2024


Author: smanna12
Date: 2024-07-01T10:51:28-05:00
New Revision: 05d8ea77c9bbdedc462dadfdcc41332253c1c829

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

LOG: [Clang] Prevent null pointer dereferences in SVE tuple functions (#94267)

This patch 

addresses a null pointer dereference issue reported by static analyzer
tool in the
`EmitSVETupleSetOrGet()` and `EmitSVETupleCreate()` functions.
Previously, the function
assumed that the result of `dyn_cast<>` to `ScalableVectorType` would
always be non-null,
    which is not guaranteed.

The fix introduces a null check after the `dyn_cast<>` operation. If the
cast fails and
`SingleVecTy` is null, the function now returns `nullptr` to indicate an
error. This prevents the
  dereference of a null pointer, which could lead to undefined behavior.

Additionally, the assert message has been corrected to accurately
reflect the expected
   conditions.

These changes collectively enhance the robustness of the code by
ensuring type safety and preventing runtime errors due to improper type
casting.

Added: 
    

Modified: 
    clang/lib/CodeGen/CGBuiltin.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 98c2f70664ec7..ed37267efe715 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -10246,11 +10246,15 @@ Value *CodeGenFunction::EmitSVETupleSetOrGet(const SVETypeFlags &TypeFlags,
                                              llvm::Type *Ty,
                                              ArrayRef<Value *> Ops) {
   assert((TypeFlags.isTupleSet() || TypeFlags.isTupleGet()) &&
-         "Expects TypleFlag isTupleSet or TypeFlags.isTupleSet()");
+         "Expects TypleFlags.isTupleSet() or TypeFlags.isTupleGet()");
 
   unsigned I = cast<ConstantInt>(Ops[1])->getSExtValue();
   auto *SingleVecTy = dyn_cast<llvm::ScalableVectorType>(
-                      TypeFlags.isTupleSet() ? Ops[2]->getType() : Ty);
+      TypeFlags.isTupleSet() ? Ops[2]->getType() : Ty);
+
+  if (!SingleVecTy)
+    return nullptr;
+
   Value *Idx = ConstantInt::get(CGM.Int64Ty,
                                 I * SingleVecTy->getMinNumElements());
 
@@ -10265,6 +10269,10 @@ Value *CodeGenFunction::EmitSVETupleCreate(const SVETypeFlags &TypeFlags,
   assert(TypeFlags.isTupleCreate() && "Expects TypleFlag isTupleCreate");
 
   auto *SrcTy = dyn_cast<llvm::ScalableVectorType>(Ops[0]->getType());
+
+  if (!SrcTy)
+    return nullptr;
+
   unsigned MinElts = SrcTy->getMinNumElements();
   Value *Call = llvm::PoisonValue::get(Ty);
   for (unsigned I = 0; I < Ops.size(); I++) {


        


More information about the cfe-commits mailing list