diff options
author | Zhongxing Xu <xuzhongxing@gmail.com> | 2009-03-11 07:43:49 +0000 |
---|---|---|
committer | Zhongxing Xu <xuzhongxing@gmail.com> | 2009-03-11 07:43:49 +0000 |
commit | 507202ecb79bf1da7ec2b3e854b78ce660a68012 (patch) | |
tree | 3a1b140bcc6174f0077083c84e2cc34f532b98da | |
parent | 664cf27602f1a50381d565952502eaf9d8246a21 (diff) | |
download | bcm5719-llvm-507202ecb79bf1da7ec2b3e854b78ce660a68012.tar.gz bcm5719-llvm-507202ecb79bf1da7ec2b3e854b78ce660a68012.zip |
Fix crash when LHS of pointer arithmetic is not ElementRegion.
llvm-svn: 66649
-rw-r--r-- | clang/lib/Analysis/RegionStore.cpp | 18 | ||||
-rw-r--r-- | clang/test/Analysis/ptr-arith.c | 7 |
2 files changed, 22 insertions, 3 deletions
diff --git a/clang/lib/Analysis/RegionStore.cpp b/clang/lib/Analysis/RegionStore.cpp index 6253e6182f4..883821128af 100644 --- a/clang/lib/Analysis/RegionStore.cpp +++ b/clang/lib/Analysis/RegionStore.cpp @@ -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); diff --git a/clang/test/Analysis/ptr-arith.c b/clang/test/Analysis/ptr-arith.c index a8d03eb3d88..7b66b2f8fe3 100644 --- a/clang/test/Analysis/ptr-arith.c +++ b/clang/test/Analysis/ptr-arith.c @@ -5,3 +5,10 @@ void f1() { int *p = a; ++p; } + +char* foo(); + +void f2() { + char *p = foo(); + ++p; +} |