[clang] d258196 - [clang] ScalarExprEmitter::VisitCastExpr - use castAs<> instead of getAs<> to avoid dereference of nullptr

Simon Pilgrim via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 9 03:40:57 PST 2022


Author: Simon Pilgrim
Date: 2022-03-09T11:40:37Z
New Revision: d258196f5fddf510e785be68da803d5feae56855

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

LOG: [clang] ScalarExprEmitter::VisitCastExpr - use castAs<> instead of getAs<> to avoid dereference of nullptr

The pointers are always dereferenced, so assert the cast is correct instead of returning nullptr

Added: 
    

Modified: 
    clang/lib/CodeGen/CGExprScalar.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 93fb7d37c3445..f0692a2266b74 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -2039,12 +2039,12 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
     }
 
     if (CGF.SanOpts.has(SanitizerKind::CFIUnrelatedCast)) {
-      if (auto PT = DestTy->getAs<PointerType>()) {
+      if (auto *PT = DestTy->getAs<PointerType>()) {
         CGF.EmitVTablePtrCheckForCast(
             PT->getPointeeType(),
             Address(Src,
                     CGF.ConvertTypeForMem(
-                        E->getType()->getAs<PointerType>()->getPointeeType()),
+                        E->getType()->castAs<PointerType>()->getPointeeType()),
                     CGF.getPointerAlign()),
             /*MayBeNull=*/true, CodeGenFunction::CFITCK_UnrelatedCast,
             CE->getBeginLoc());
@@ -2948,8 +2948,8 @@ Value *ScalarExprEmitter::VisitOffsetOfExpr(OffsetOfExpr *E) {
       CurrentType = ON.getBase()->getType();
 
       // Compute the offset to the base.
-      const RecordType *BaseRT = CurrentType->getAs<RecordType>();
-      CXXRecordDecl *BaseRD = cast<CXXRecordDecl>(BaseRT->getDecl());
+      auto *BaseRT = CurrentType->castAs<RecordType>();
+      auto *BaseRD = cast<CXXRecordDecl>(BaseRT->getDecl());
       CharUnits OffsetInt = RL.getBaseClassOffset(BaseRD);
       Offset = llvm::ConstantInt::get(ResultType, OffsetInt.getQuantity());
       break;


        


More information about the cfe-commits mailing list