[clang] [clang][dataflow] Fix crash on base-to-derived cast of unmodeled pointer value. (PR #179060)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Jan 31 10:40:53 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Artem Dergachev (haoNoQ)
<details>
<summary>Changes</summary>
Some values aren't modeled yet and that's ok.
Conjure a value in case we need it later.
---
Full diff: https://github.com/llvm/llvm-project/pull/179060.diff
2 Files Affected:
- (modified) clang/lib/Analysis/FlowSensitive/Transfer.cpp (+2-2)
- (modified) clang/unittests/Analysis/FlowSensitive/TransferTest.cpp (+27)
``````````diff
diff --git a/clang/lib/Analysis/FlowSensitive/Transfer.cpp b/clang/lib/Analysis/FlowSensitive/Transfer.cpp
index 51cc1f9bc26ab..29b07848841ad 100644
--- a/clang/lib/Analysis/FlowSensitive/Transfer.cpp
+++ b/clang/lib/Analysis/FlowSensitive/Transfer.cpp
@@ -328,9 +328,9 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
RecordStorageLocation *Loc = nullptr;
if (S->getType()->isPointerType()) {
auto *PV = Env.get<PointerValue>(*SubExpr);
- assert(PV != nullptr);
if (PV == nullptr)
- break;
+ PV = cast<PointerValue>(Env.createValue(S->getType()));
+
Loc = cast<RecordStorageLocation>(&PV->getPointeeLoc());
} else {
assert(S->getType()->isRecordType());
diff --git a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
index e528ca2221ad1..142beb2f150ba 100644
--- a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
@@ -3786,6 +3786,33 @@ TEST(TransferTest, StaticCastBaseToDerived) {
});
}
+TEST(TransferTest, StaticCastBaseToDerivedUnknown) {
+ std::string Code = R"(
+ struct Base {};
+ struct Derived: Base {};
+
+ Base *unknown();
+ void target() {
+ Derived *DPtr = static_cast<Derived *>(unknown());
+ // [[p]]
+ }
+ )";
+ runDataflow(
+ Code,
+ [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
+ ASTContext &ASTCtx) {
+ ASSERT_THAT(Results.keys(), UnorderedElementsAre("p"));
+ const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
+
+ const ValueDecl *DPtrDecl = findValueDecl(ASTCtx, "DPtr");
+ ASSERT_THAT(DPtrDecl, NotNull());
+
+ const auto *DPtrVal =
+ dyn_cast_or_null<PointerValue>(Env.getValue(*DPtrDecl));
+ EXPECT_THAT(DPtrVal, NotNull());
+ });
+}
+
TEST(TransferTest, MultipleConstructionsFromStaticCastsBaseToDerived) {
std::string Code = R"cc(
struct Base {};
``````````
</details>
https://github.com/llvm/llvm-project/pull/179060
More information about the cfe-commits
mailing list