Skip to content

Commit 4389220

Browse files
authored
[Clang] Prevent potential null pointer dereferences (llvm#117176)
This commit addresses several null pointer issues identified by static analysis by replacing dyn_cast<> with cast<> and getAs<> with castAs<> in various parts of the Clang codebase. The cast and castAs method is used to ensure that the type is correctly cast, which helps prevent potential null pointer dereferences. Changes: 1. ASTContext.cpp: Replaced dyn_cast with cast to ensure that the type is correctly cast to AttributedType. 2. SemaFunctionEffects.cpp: Replaced getAs with castAs to ensure that the type is correctly cast to FunctionProtoType. 3. SemaHLSL.cpp: Replaced getAs with castAs to ensure that the type is correctly cast to VectorType.
1 parent ecaf2c3 commit 4389220

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

clang/lib/AST/ASTContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3558,7 +3558,7 @@ ASTContext::adjustType(QualType Orig,
35583558
llvm::function_ref<QualType(QualType)> Adjust) const {
35593559
switch (Orig->getTypeClass()) {
35603560
case Type::Attributed: {
3561-
const auto *AT = dyn_cast<AttributedType>(Orig);
3561+
const auto *AT = cast<AttributedType>(Orig);
35623562
return getAttributedType(AT->getAttrKind(),
35633563
adjustType(AT->getModifiedType(), Adjust),
35643564
adjustType(AT->getEquivalentType(), Adjust),

clang/lib/Sema/SemaFunctionEffects.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ class Analyzer {
627627
IsNoexcept = isNoexcept(FD);
628628
} else if (auto *BD = dyn_cast<BlockDecl>(D)) {
629629
if (auto *TSI = BD->getSignatureAsWritten()) {
630-
auto *FPT = TSI->getType()->getAs<FunctionProtoType>();
630+
auto *FPT = TSI->getType()->castAs<FunctionProtoType>();
631631
IsNoexcept = FPT->isNothrow() || BD->hasAttr<NoThrowAttr>();
632632
}
633633
}

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,9 +1908,9 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
19081908
return true;
19091909
// ensure both args have 3 elements
19101910
int NumElementsArg1 =
1911-
TheCall->getArg(0)->getType()->getAs<VectorType>()->getNumElements();
1911+
TheCall->getArg(0)->getType()->castAs<VectorType>()->getNumElements();
19121912
int NumElementsArg2 =
1913-
TheCall->getArg(1)->getType()->getAs<VectorType>()->getNumElements();
1913+
TheCall->getArg(1)->getType()->castAs<VectorType>()->getNumElements();
19141914

19151915
if (NumElementsArg1 != 3) {
19161916
int LessOrMore = NumElementsArg1 > 3 ? 1 : 0;

0 commit comments

Comments
 (0)