Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dataflow] Fix crash when InitListExpr is not a prvalue #80970

Merged

Conversation

paulsemel
Copy link
Contributor

No description provided.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:dataflow Clang Dataflow Analysis framework - https://clang.llvm.org/docs/DataFlowAnalysisIntro.html clang:analysis labels Feb 7, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Feb 7, 2024

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-analysis

Author: Paul Semel (paulsemel)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/80970.diff

2 Files Affected:

  • (modified) clang/lib/Analysis/FlowSensitive/Transfer.cpp (+6)
  • (modified) clang/unittests/Analysis/FlowSensitive/TransferTest.cpp (+14)
diff --git a/clang/lib/Analysis/FlowSensitive/Transfer.cpp b/clang/lib/Analysis/FlowSensitive/Transfer.cpp
index bb3aec763c29ca..dc0f1bd6dcc291 100644
--- a/clang/lib/Analysis/FlowSensitive/Transfer.cpp
+++ b/clang/lib/Analysis/FlowSensitive/Transfer.cpp
@@ -648,6 +648,12 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
     QualType Type = S->getType();
 
     if (!Type->isStructureOrClassType()) {
+      // It is possible that InitListExpr is not a prvalue, in which case
+      // `setValue` will fail. In this case, we can just let the next
+      // transfer function handle the value creation.
+      if (!S->isPRValue())
+        return;
+
       if (auto *Val = Env.createValue(Type))
         Env.setValue(*S, *Val);
 
diff --git a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
index 8bbb04024dcce6..74e6f1abfa4ba3 100644
--- a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
@@ -2313,6 +2313,20 @@ TEST(TransferTest, AssignmentOperatorWithInitAndInheritance) {
          ASTContext &ASTCtx) {});
 }
 
+TEST(TransferTest, InitListExprAsXValue) {
+  // This is a crash repro.
+  std::string Code = R"(
+    void target() {
+      auto&& test{false};
+      // [[p]]
+    }
+  )";
+  runDataflow(
+      Code,
+      [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
+         ASTContext &ASTCtx) {});
+}
+
 TEST(TransferTest, CopyConstructor) {
   std::string Code = R"(
     struct A {

@paulsemel paulsemel force-pushed the fix-init-list-expr-not-being-prvalues branch from e56ced7 to 1ff7ea5 Compare February 8, 2024 15:08
@paulsemel paulsemel force-pushed the fix-init-list-expr-not-being-prvalues branch from 1ff7ea5 to 35fa8c1 Compare February 8, 2024 15:54
@paulsemel paulsemel force-pushed the fix-init-list-expr-not-being-prvalues branch from 8b4d49d to 584e875 Compare February 13, 2024 11:49
@paulsemel paulsemel merged commit ba27993 into llvm:main Feb 15, 2024
5 checks passed
bazuzi added a commit to bazuzi/llvm-project that referenced this pull request Feb 26, 2024
Crashes resulted from single-element InitListExprs for arrays with
elements of a record type after llvm#80970.
bazuzi added a commit to bazuzi/llvm-project that referenced this pull request Feb 26, 2024
Crashes resulted from single-element InitListExprs for arrays with
elements of a record type after llvm#80970.
ymand pushed a commit that referenced this pull request Feb 26, 2024
Crashes resulted from single-element InitListExprs for arrays with
elements of a record type after #80970.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:analysis clang:dataflow Clang Dataflow Analysis framework - https://clang.llvm.org/docs/DataFlowAnalysisIntro.html clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants