[clang][dataflow] Don't crash if copy constructor arg doesn't have a storage location.
authorMartin Braenne <mboehme@google.com>
Wed, 28 Jun 2023 09:16:09 +0000 (09:16 +0000)
committerMartin Braenne <mboehme@google.com>
Wed, 28 Jun 2023 11:15:52 +0000 (11:15 +0000)
I accidentally used `cast` instead of `cast_or_null`.

Reviewed By: sammccall, xazax.hun

Differential Revision: https://reviews.llvm.org/D153956

clang/lib/Analysis/FlowSensitive/Transfer.cpp
clang/unittests/Analysis/FlowSensitive/TransferTest.cpp

index c09b6b9..54b8b3a 100644 (file)
@@ -598,7 +598,7 @@ public:
       const Expr *Arg = S->getArg(0);
       assert(Arg != nullptr);
 
-      auto *ArgLoc = cast<AggregateStorageLocation>(
+      auto *ArgLoc = cast_or_null<AggregateStorageLocation>(
           Env.getStorageLocation(*Arg, SkipPast::Reference));
       if (ArgLoc == nullptr)
         return;
index 51550ad..f41c3f2 100644 (file)
@@ -2237,6 +2237,21 @@ TEST(TransferTest, CopyConstructorWithParens) {
       });
 }
 
+TEST(TransferTest, CopyConstructorArgIsRefReturnedByFunction) {
+  // This is a crash repro.
+  std::string Code = R"(
+    struct S {};
+    const S &returnsSRef();
+    void target() {
+      S s(returnsSRef());
+    }
+  )";
+  runDataflow(
+      Code,
+      [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
+         ASTContext &ASTCtx) {});
+}
+
 TEST(TransferTest, MoveConstructor) {
   std::string Code = R"(
     namespace std {