[clang] [clang][analysis]Use dyn_cast_or_null instead cast to prevent crash (PR #68510)

Qizhi Hu via cfe-commits cfe-commits at lists.llvm.org
Sun Oct 8 01:08:08 PDT 2023


https://github.com/jcsxky created https://github.com/llvm/llvm-project/pull/68510

`getStorageLocation` may return `nullptr` and this will produce crash when use `cast`, use `dyn_cast_or_null` instead. I test it locally using [FTXUI](https://github.com/ArthurSonzogni/FTXUI) and it may be the cause of issue [issue](https://github.com/llvm/llvm-project/issues/68412), but I am not sure.

>From 870856a94dbf10390baaca703d48f988ce070979 Mon Sep 17 00:00:00 2001
From: huqizhi <huqizhi at feysh.com>
Date: Sun, 8 Oct 2023 16:00:29 +0800
Subject: [PATCH] [clang][analysis]Use dyn_cast_or_null instead cast to prevent
 crash

---
 .../FlowSensitive/Models/UncheckedOptionalAccessModel.cpp      | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp b/clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp
index f61f26ff27804ec..dda1c6cfca017d0 100644
--- a/clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp
+++ b/clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp
@@ -26,6 +26,7 @@
 #include "clang/Analysis/FlowSensitive/NoopLattice.h"
 #include "clang/Analysis/FlowSensitive/StorageLocation.h"
 #include "clang/Analysis/FlowSensitive/Value.h"
+#include "clang/Basic/LLVM.h"
 #include "clang/Basic/SourceLocation.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Casting.h"
@@ -599,7 +600,7 @@ void transferAssignment(const CXXOperatorCallExpr *E, BoolValue &HasValueVal,
                         LatticeTransferState &State) {
   assert(E->getNumArgs() > 0);
 
-  if (auto *Loc = cast<RecordStorageLocation>(
+  if (auto *Loc = dyn_cast_or_null<RecordStorageLocation>(
           State.Env.getStorageLocation(*E->getArg(0)))) {
     createOptionalValue(*Loc, HasValueVal, State.Env);
 



More information about the cfe-commits mailing list