[clang] [dataflow] Fix crash when InitListExpr is not a prvalue (PR #80970)

Paul Semel via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 8 07:08:48 PST 2024


https://github.com/paulsemel updated https://github.com/llvm/llvm-project/pull/80970

>From 1ff7ea57e76041b25889685ead115a1b793d3921 Mon Sep 17 00:00:00 2001
From: Paul Semel <semelpaul at gmail.com>
Date: Wed, 7 Feb 2024 10:26:23 +0000
Subject: [PATCH] [dataflow] Fix crash when InitListExpr is not a prvalue

---
 clang/lib/Analysis/FlowSensitive/Transfer.cpp |  5 ++---
 .../Analysis/FlowSensitive/TransferTest.cpp   | 22 +++++++++++++++++++
 2 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Analysis/FlowSensitive/Transfer.cpp b/clang/lib/Analysis/FlowSensitive/Transfer.cpp
index bb3aec763c29c..c299524a58d38 100644
--- a/clang/lib/Analysis/FlowSensitive/Transfer.cpp
+++ b/clang/lib/Analysis/FlowSensitive/Transfer.cpp
@@ -648,9 +648,8 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
     QualType Type = S->getType();
 
     if (!Type->isStructureOrClassType()) {
-      if (auto *Val = Env.createValue(Type))
-        Env.setValue(*S, *Val);
-
+      assert(S->getNumInits() == 1);
+      propagateValueOrStorageLocation(*S->getInit(0), *S, Env);
       return;
     }
 
diff --git a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
index 8bbb04024dcce..cda92bc421f6c 100644
--- a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
@@ -2313,6 +2313,28 @@ TEST(TransferTest, AssignmentOperatorWithInitAndInheritance) {
          ASTContext &ASTCtx) {});
 }
 
+TEST(TransferTest, InitListExprAsXValue) {
+  // This is a crash repro.
+  std::string Code = R"(
+    void target() {
+      bool&& Foo{false};
+      // [[p]]
+    }
+  )";
+  runDataflow(
+      Code,
+      [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
+         ASTContext &ASTCtx) {
+        const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
+        ASSERT_THAT(FooDecl, NotNull());
+        const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
+        const auto *FooVal =
+            dyn_cast_or_null<BoolValue>(Env.getValue(*FooDecl));
+        ASSERT_THAT(FooVal, NotNull());
+        ASSERT_EQ(FooVal, &Env.getBoolLiteralValue(false));
+      });
+}
+
 TEST(TransferTest, CopyConstructor) {
   std::string Code = R"(
     struct A {



More information about the cfe-commits mailing list