-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[Clang] Prevent null pointer dereferences in SVE tuple functions #94267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This patch replaces dyn_cast<> with cast<> for obtaining ScalableVectorType pointers in the EmitSVETupleSetOrGet() and EmitSVETupleCreate() functions to ensure that the code expects valid ScalableVectorType instances and will cause an assertion if the cast is invalid, preventing possible null pointer dereferences and improving codes safety.
@llvm/pr-subscribers-clang @llvm/pr-subscribers-clang-codegen Author: None (smanna12) ChangesThis patch replaces dyn_cast<> with cast<> for obtaining ScalableVectorType pointers in the EmitSVETupleSetOrGet() and EmitSVETupleCreate() functions to ensure that the code expects valid ScalableVectorType instances and will cause an assertion if the cast is invalid, preventing possible null pointer dereferences and improving codes safety. Full diff: https://github.com/llvm/llvm-project/pull/94267.diff 1 Files Affected:
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 37d0c478e0330..3ba9f4693170f 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -10211,7 +10211,7 @@ Value *CodeGenFunction::EmitSVETupleSetOrGet(const SVETypeFlags &TypeFlags,
"Expects TypleFlag isTupleSet or TypeFlags.isTupleSet()");
unsigned I = cast<ConstantInt>(Ops[1])->getSExtValue();
- auto *SingleVecTy = dyn_cast<llvm::ScalableVectorType>(
+ auto *SingleVecTy = cast<llvm::ScalableVectorType>(
TypeFlags.isTupleSet() ? Ops[2]->getType() : Ty);
Value *Idx = ConstantInt::get(CGM.Int64Ty,
I * SingleVecTy->getMinNumElements());
@@ -10226,7 +10226,7 @@ Value *CodeGenFunction::EmitSVETupleCreate(const SVETypeFlags &TypeFlags,
ArrayRef<Value *> Ops) {
assert(TypeFlags.isTupleCreate() && "Expects TypleFlag isTupleCreate");
- auto *SrcTy = dyn_cast<llvm::ScalableVectorType>(Ops[0]->getType());
+ auto *SrcTy = cast<llvm::ScalableVectorType>(Ops[0]->getType());
unsigned MinElts = SrcTy->getMinNumElements();
Value *Call = llvm::PoisonValue::get(Ty);
for (unsigned I = 0; I < Ops.size(); I++) {
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @smanna12.
Thank you @CarolineConcatto for reviews! |
…m#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.
…m#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.
This patch
addresses a null pointer dereference issue reported by static analyzer tool in the
EmitSVETupleSetOrGet()
andEmitSVETupleCreate()
functions. Previously, the functionassumed that the result of
dyn_cast<>
toScalableVectorType
would always be non-null,which is not guaranteed.
The fix introduces a null check after the
dyn_cast<>
operation. If the cast fails andSingleVecTy
is null, the function now returnsnullptr
to indicate an error. This prevents thedereference 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.