Skip to content

Commit 7b61ff2

Browse files
authored
[Clang] Prevent null dereferences (#115502)
This commit addresses several Static Analyzer issues related to potential null dereference by replacing dyn_cast<> with cast<> and getAs<> with castAs<> in various parts of the codes. The cast function asserts that the cast is valid, ensuring that the pointer is not null and preventing null dereference errors. The changes are made in the following files: CGBuiltin.cpp: Ensure vector types have exactly 3 elements. CGExpr.cpp: Ensure member declarations are field declarations. AnalysisBasedWarnings.cpp: Ensure operations are member expressions. SemaExprMember.cpp: Ensure base types are extended vector types. These changes ensure that the types are correctly cast and prevent potential null dereference issues, improving the robustness and safety of the code.
1 parent e8b5c00 commit 7b61ff2

File tree

4 files changed

+5
-5
lines changed

4 files changed

+5
-5
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19052,8 +19052,8 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
1905219052
"cross operands must have a float representation");
1905319053
// make sure each vector has exactly 3 elements
1905419054
assert(
19055-
E->getArg(0)->getType()->getAs<VectorType>()->getNumElements() == 3 &&
19056-
E->getArg(1)->getType()->getAs<VectorType>()->getNumElements() == 3 &&
19055+
E->getArg(0)->getType()->castAs<VectorType>()->getNumElements() == 3 &&
19056+
E->getArg(1)->getType()->castAs<VectorType>()->getNumElements() == 3 &&
1905719057
"input vectors must have 3 elements each");
1905819058
return Builder.CreateIntrinsic(
1905919059
/*ReturnType=*/Op0->getType(), CGM.getHLSLRuntime().getCrossIntrinsic(),

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4370,7 +4370,7 @@ LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E,
43704370
ME &&
43714371
ME->isFlexibleArrayMemberLike(getContext(), StrictFlexArraysLevel) &&
43724372
ME->getMemberDecl()->getType()->isCountAttributedType()) {
4373-
const FieldDecl *FAMDecl = dyn_cast<FieldDecl>(ME->getMemberDecl());
4373+
const FieldDecl *FAMDecl = cast<FieldDecl>(ME->getMemberDecl());
43744374
if (const FieldDecl *CountFD = FAMDecl->findCountedByField()) {
43754375
if (std::optional<int64_t> Diff =
43764376
getOffsetDifferenceInBits(*this, CountFD, FAMDecl)) {

clang/lib/Sema/AnalysisBasedWarnings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2257,7 +2257,7 @@ class UnsafeBufferUsageReporter : public UnsafeBufferUsageHandler {
22572257
} else if (isa<MemberExpr>(Operation)) {
22582258
// note_unsafe_buffer_operation doesn't have this mode yet.
22592259
assert(!IsRelatedToDecl && "Not implemented yet!");
2260-
auto ME = dyn_cast<MemberExpr>(Operation);
2260+
auto *ME = cast<MemberExpr>(Operation);
22612261
D = ME->getMemberDecl();
22622262
MsgParam = 5;
22632263
} else if (const auto *ECE = dyn_cast<ExplicitCastExpr>(Operation)) {

clang/lib/Sema/SemaExprMember.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ CheckExtVectorComponent(Sema &S, QualType baseType, ExprValueKind &VK,
377377
//
378378
// FIXME: This logic can be greatly simplified by splitting it along
379379
// halving/not halving and reworking the component checking.
380-
const ExtVectorType *vecType = baseType->getAs<ExtVectorType>();
380+
const ExtVectorType *vecType = baseType->castAs<ExtVectorType>();
381381

382382
// The vector accessor can't exceed the number of elements.
383383
const char *compStr = CompName->getNameStart();

0 commit comments

Comments
 (0)