Skip to content

Commit 05d8ea7

Browse files
authored
[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.
1 parent e8574e8 commit 05d8ea7

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10246,11 +10246,15 @@ Value *CodeGenFunction::EmitSVETupleSetOrGet(const SVETypeFlags &TypeFlags,
1024610246
llvm::Type *Ty,
1024710247
ArrayRef<Value *> Ops) {
1024810248
assert((TypeFlags.isTupleSet() || TypeFlags.isTupleGet()) &&
10249-
"Expects TypleFlag isTupleSet or TypeFlags.isTupleSet()");
10249+
"Expects TypleFlags.isTupleSet() or TypeFlags.isTupleGet()");
1025010250

1025110251
unsigned I = cast<ConstantInt>(Ops[1])->getSExtValue();
1025210252
auto *SingleVecTy = dyn_cast<llvm::ScalableVectorType>(
10253-
TypeFlags.isTupleSet() ? Ops[2]->getType() : Ty);
10253+
TypeFlags.isTupleSet() ? Ops[2]->getType() : Ty);
10254+
10255+
if (!SingleVecTy)
10256+
return nullptr;
10257+
1025410258
Value *Idx = ConstantInt::get(CGM.Int64Ty,
1025510259
I * SingleVecTy->getMinNumElements());
1025610260

@@ -10265,6 +10269,10 @@ Value *CodeGenFunction::EmitSVETupleCreate(const SVETypeFlags &TypeFlags,
1026510269
assert(TypeFlags.isTupleCreate() && "Expects TypleFlag isTupleCreate");
1026610270

1026710271
auto *SrcTy = dyn_cast<llvm::ScalableVectorType>(Ops[0]->getType());
10272+
10273+
if (!SrcTy)
10274+
return nullptr;
10275+
1026810276
unsigned MinElts = SrcTy->getMinNumElements();
1026910277
Value *Call = llvm::PoisonValue::get(Ty);
1027010278
for (unsigned I = 0; I < Ops.size(); I++) {

0 commit comments

Comments
 (0)