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

Paul Semel via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 13 03:40:51 PST 2024


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

>From 35fa8c1f4815bf2922ba9018d3f64b99428ecdc6 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 1/2] [dataflow] Fix crash when InitListExpr is not a prvalue

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

diff --git a/clang/lib/Analysis/FlowSensitive/Transfer.cpp b/clang/lib/Analysis/FlowSensitive/Transfer.cpp
index bb3aec763c29ca..993fabca5e5e65 100644
--- a/clang/lib/Analysis/FlowSensitive/Transfer.cpp
+++ b/clang/lib/Analysis/FlowSensitive/Transfer.cpp
@@ -648,9 +648,10 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
     QualType Type = S->getType();
 
     if (!Type->isStructureOrClassType()) {
-      if (auto *Val = Env.createValue(Type))
-        Env.setValue(*S, *Val);
-
+      // Until array initialization is implemented, we don't need to care about
+      // cases where `getNumInits() > 1`.
+      if (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 8bbb04024dcce6..cda92bc421f6c6 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 {

>From 8b4d49dc9f22e804ee853eb7ea4980641d7f0da7 Mon Sep 17 00:00:00 2001
From: Paul Semel <paul.semel at epita.fr>
Date: Tue, 13 Feb 2024 12:40:45 +0100
Subject: [PATCH 2/2] Update
 clang/unittests/Analysis/FlowSensitive/TransferTest.cpp

Co-authored-by: martinboehme <mboehme at google.com>
---
 clang/unittests/Analysis/FlowSensitive/TransferTest.cpp | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
index cda92bc421f6c6..da76040b2116bd 100644
--- a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
@@ -2325,13 +2325,9 @@ TEST(TransferTest, InitListExprAsXValue) {
       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));
+        const auto &FooVal = getValueForDecl<BoolValue>(ASTCtx, Env, "Foo");
+        ASSERT_TRUE(FooVal.formula().isLiteral(false));
       });
 }
 



More information about the cfe-commits mailing list