[cfe-commits] r66649 - in /cfe/trunk: lib/Analysis/RegionStore.cpp test/Analysis/ptr-arith.c

Zhongxing Xu xuzhongxing at gmail.com
Wed Mar 11 00:43:49 PDT 2009


Author: zhongxingxu
Date: Wed Mar 11 02:43:49 2009
New Revision: 66649

URL: http://llvm.org/viewvc/llvm-project?rev=66649&view=rev
Log:
Fix crash when LHS of pointer arithmetic is not ElementRegion.

Modified:
    cfe/trunk/lib/Analysis/RegionStore.cpp
    cfe/trunk/test/Analysis/ptr-arith.c

Modified: cfe/trunk/lib/Analysis/RegionStore.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/RegionStore.cpp?rev=66649&r1=66648&r2=66649&view=diff

==============================================================================
--- cfe/trunk/lib/Analysis/RegionStore.cpp (original)
+++ cfe/trunk/lib/Analysis/RegionStore.cpp Wed Mar 11 02:43:49 2009
@@ -620,9 +620,21 @@
   if (!isa<loc::MemRegionVal>(L))
     return UnknownVal();
 
-  const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
+  const TypedRegion* TR 
+    = cast<TypedRegion>(cast<loc::MemRegionVal>(L).getRegion());
+
+  const ElementRegion* ER = dyn_cast<ElementRegion>(TR);
+  
+  if (!ER) {
+    // If the region is not element region, create one with index 0. This can
+    // happen in the following example:
+    // char *p = foo();
+    // p += 3;
+    // Note that p binds to a TypedViewRegion(SymbolicRegion).
+    nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
+    ER = MRMgr.getElementRegion(Idx, TR);
+  }
 
-  const ElementRegion* ER = cast<ElementRegion>(MR);
   SVal Idx = ER->getIndex();
 
   nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
@@ -632,7 +644,7 @@
   if (Base && Offset) {
     // For now, convert the signedness of offset in case it doesn't match.
     const llvm::APSInt &I =
-      getBasicVals().ConvertSignedness(Base->getValue(), Offset->getValue());    
+      getBasicVals().ConvertSignedness(Base->getValue(), Offset->getValue());
     nonloc::ConcreteInt OffsetConverted(I);
     
     SVal NewIdx = Base->EvalBinOp(getBasicVals(), Op, OffsetConverted);

Modified: cfe/trunk/test/Analysis/ptr-arith.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/ptr-arith.c?rev=66649&r1=66648&r2=66649&view=diff

==============================================================================
--- cfe/trunk/test/Analysis/ptr-arith.c (original)
+++ cfe/trunk/test/Analysis/ptr-arith.c Wed Mar 11 02:43:49 2009
@@ -5,3 +5,10 @@
   int *p = a;
   ++p;
 }
+
+char* foo();
+
+void f2() {
+  char *p = foo();
+  ++p;
+}





More information about the cfe-commits mailing list