diff options
author | Rafael Stahl <r.stahl@tum.de> | 2018-05-29 14:14:22 +0000 |
---|---|---|
committer | Rafael Stahl <r.stahl@tum.de> | 2018-05-29 14:14:22 +0000 |
commit | 0137aa8679d6675b2440c1e19943b23cee4dc794 (patch) | |
tree | c7659e120ba6d9bdabab409664555a469217fb9b /clang/lib | |
parent | 48bf43df8a92057feaad1142d0e251f7a160ab12 (diff) | |
download | bcm5719-llvm-0137aa8679d6675b2440c1e19943b23cee4dc794.tar.gz bcm5719-llvm-0137aa8679d6675b2440c1e19943b23cee4dc794.zip |
[analyzer] const init: handle non-explicit cases more accurately
Summary: If the access is out of bounds, return UndefinedVal. If it is missing an explicit init, return the implicit zero value it must have.
Reviewers: NoQ, xazax.hun, george.karpenkov
Reviewed By: NoQ
Subscribers: szepet, rnkovacs, a.sidorin, cfe-commits
Differential Revision: https://reviews.llvm.org/D46823
llvm-svn: 333417
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/StaticAnalyzer/Core/RegionStore.cpp | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/clang/lib/StaticAnalyzer/Core/RegionStore.cpp b/clang/lib/StaticAnalyzer/Core/RegionStore.cpp index be4b5aa9189..acb6eeab8d9 100644 --- a/clang/lib/StaticAnalyzer/Core/RegionStore.cpp +++ b/clang/lib/StaticAnalyzer/Core/RegionStore.cpp @@ -1638,9 +1638,18 @@ SVal RegionStoreManager::getBindingForElement(RegionBindingsConstRef B, // The array index has to be known. if (auto CI = R->getIndex().getAs<nonloc::ConcreteInt>()) { int64_t i = CI->getValue().getSExtValue(); - // Return unknown value if index is out of bounds. - if (i < 0 || i >= InitList->getNumInits()) - return UnknownVal(); + // If it is known that the index is out of bounds, we can return + // an undefined value. + if (i < 0) + return UndefinedVal(); + + if (auto CAT = Ctx.getAsConstantArrayType(VD->getType())) + if (CAT->getSize().sle(i)) + return UndefinedVal(); + + // If there is a list, but no init, it must be zero. + if (i >= InitList->getNumInits()) + return svalBuilder.makeZeroVal(R->getElementType()); if (const Expr *ElemInit = InitList->getInit(i)) if (Optional<SVal> V = svalBuilder.getConstantVal(ElemInit)) @@ -1715,11 +1724,15 @@ SVal RegionStoreManager::getBindingForField(RegionBindingsConstRef B, // Either the record variable or the field has to be const qualified. if (RecordVarTy.isConstQualified() || Ty.isConstQualified()) if (const Expr *Init = VD->getInit()) - if (const auto *InitList = dyn_cast<InitListExpr>(Init)) - if (Index < InitList->getNumInits()) + if (const auto *InitList = dyn_cast<InitListExpr>(Init)) { + if (Index < InitList->getNumInits()) { if (const Expr *FieldInit = InitList->getInit(Index)) if (Optional<SVal> V = svalBuilder.getConstantVal(FieldInit)) return *V; + } else { + return svalBuilder.makeZeroVal(Ty); + } + } } return getBindingForFieldOrElementCommon(B, R, Ty); |