]> granicus.if.org Git - clang/commitdiff
Fix crash when analyzing C++ code involving constant enums and switch statements...
authorTed Kremenek <kremenek@apple.com>
Fri, 30 Sep 2011 03:51:54 +0000 (03:51 +0000)
committerTed Kremenek <kremenek@apple.com>
Fri, 30 Sep 2011 03:51:54 +0000 (03:51 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@140844 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Analysis/ProgramPoint.h
lib/StaticAnalyzer/Core/CoreEngine.cpp
test/Analysis/misc-ps-region-store.cpp

index 8f2b55c9a99949ed289942f39ff677941237b691..2ff2be9fa13408331529ca441642412d497fde78 100644 (file)
@@ -301,7 +301,10 @@ public:
 class BlockEdge : public ProgramPoint {
 public:
   BlockEdge(const CFGBlock *B1, const CFGBlock *B2, const LocationContext *L)
-    : ProgramPoint(B1, B2, BlockEdgeKind, L) {}
+    : ProgramPoint(B1, B2, BlockEdgeKind, L) {
+    assert(B1 && "BlockEdge: source block must be non-null");
+    assert(B2 && "BlockEdge: destination block must be non-null");    
+  }
 
   const CFGBlock *getSrc() const {
     return static_cast<const CFGBlock*>(getData1());
index 20b86a8fd322cc5f178b0b2abf8142c55785841a..2511ca778dd2229d7872398811aa82fb03833a52 100644 (file)
@@ -697,13 +697,18 @@ ExplodedNode*
 SwitchNodeBuilder::generateDefaultCaseNode(const ProgramState *St,
                                            bool isSink) {
   // Get the block for the default case.
-  assert (Src->succ_rbegin() != Src->succ_rend());
+  assert(Src->succ_rbegin() != Src->succ_rend());
   CFGBlock *DefaultBlock = *Src->succ_rbegin();
 
+  // Sanity check for default blocks that are unreachable and not caught
+  // by earlier stages.
+  if (!DefaultBlock)
+    return NULL;
+  
   bool IsNew;
 
   ExplodedNode *Succ = Eng.G->getNode(BlockEdge(Src, DefaultBlock,
-                                       Pred->getLocationContext()), St, &IsNew);
+                                      Pred->getLocationContext()), St, &IsNew);
   Succ->addPredecessor(Pred, *Eng.G);
 
   if (IsNew) {
index 1d055d59686fc7929d3e5cfaa0b16b5083838da8..37153f76506294fec207eaa4c37b1bb9a870185f 100644 (file)
@@ -441,3 +441,29 @@ int rdar9948787_positive() {
     return 0;
 }
 
+// Regression test against global constants and switches.
+enum rdar10202899_ValT { rdar10202899_ValTA, rdar10202899_ValTB, rdar10202899_ValTC };
+const rdar10202899_ValT val = rdar10202899_ValTA;
+void rdar10202899_test1() {
+  switch (val) {
+    case rdar10202899_ValTA: {}
+  };
+}
+
+void rdar10202899_test2() {
+  if (val == rdar10202899_ValTA)
+   return;
+  int *p = 0;
+  *p = 0xDEADBEEF;
+}
+
+void rdar10202899_test3() {
+  switch (val) {
+    case rdar10202899_ValTA: return;
+    default: ;
+  };
+  int *p = 0;
+  *p = 0xDEADBEEF;
+}
+
+