]> granicus.if.org Git - clang/commitdiff
Fix crash when LHS of pointer arithmetic is not ElementRegion.
authorZhongxing Xu <xuzhongxing@gmail.com>
Wed, 11 Mar 2009 07:43:49 +0000 (07:43 +0000)
committerZhongxing Xu <xuzhongxing@gmail.com>
Wed, 11 Mar 2009 07:43:49 +0000 (07:43 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@66649 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/RegionStore.cpp
test/Analysis/ptr-arith.c

index 6253e6182f4b76e7757bcddf080e56883ee4e985..883821128af9b8cf6106c9d2c2f23e59e9bf4c16 100644 (file)
@@ -620,9 +620,21 @@ SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R) {
   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 @@ SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R) {
   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);
index a8d03eb3d888409aa15e3b43cdb77f935ef50614..7b66b2f8fe351af2bbdd89976805e6728a8eb935 100644 (file)
@@ -5,3 +5,10 @@ void f1() {
   int *p = a;
   ++p;
 }
+
+char* foo();
+
+void f2() {
+  char *p = foo();
+  ++p;
+}