[PATCH] D30499: [analyzer] pr32088: Don't destroy the temporary if its initializer causes return.

Artem Dergachev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 1 07:25:24 PST 2017


NoQ created this revision.

In the following code involving GNU statement-expression extension:

  struct S {
    ~S();
  };
  
  void foo() {
    const S &x = ({ return; S(); });
  }

function `foo()` returns before reference `x` is initialized. We shouldn't call the destructor for the temporary object lifetime-exteneded by `x` in this case, because the object never gets constructed in the first place.

The real problem is probably in the CFG somewhere, so this is a quick-and-dirty hotfix rather than the perfect solution.


https://reviews.llvm.org/D30499

Files:
  lib/StaticAnalyzer/Core/ExprEngine.cpp
  test/Analysis/temporaries.cpp


Index: test/Analysis/temporaries.cpp
===================================================================
--- test/Analysis/temporaries.cpp
+++ test/Analysis/temporaries.cpp
@@ -493,3 +493,13 @@
     clang_analyzer_eval(x == 47); // expected-warning{{TRUE}}
   }
 }
+
+namespace PR32088 {
+  void testReturnFromStmtExprInitializer() {
+    // We shouldn't try to destroy the object pointed to by `obj' upon return.
+    const NonTrivial &obj = ({
+      return; // no-crash
+      NonTrivial(42);
+    });
+  }
+}
Index: lib/StaticAnalyzer/Core/ExprEngine.cpp
===================================================================
--- lib/StaticAnalyzer/Core/ExprEngine.cpp
+++ lib/StaticAnalyzer/Core/ExprEngine.cpp
@@ -615,7 +615,15 @@
   const MemRegion *Region = dest.castAs<loc::MemRegionVal>().getRegion();
 
   if (varType->isReferenceType()) {
-    Region = state->getSVal(Region).getAsRegion()->getBaseRegion();
+    const MemRegion *ValueRegion = state->getSVal(Region).getAsRegion();
+    if (!ValueRegion) {
+      // FIXME: This should not happen. The language guarantees a presence
+      // of a valid initializer here, so the reference shall not be undefined.
+      // It seems that we're calling destructors over variables that
+      // were not initialized yet.
+      return;
+    }
+    Region = ValueRegion->getBaseRegion();
     varType = cast<TypedValueRegion>(Region)->getValueType();
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D30499.90178.patch
Type: text/x-patch
Size: 1414 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170301/e90f8658/attachment-0001.bin>


More information about the cfe-commits mailing list