Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit 31d5dae

Browse files
committedSep 25, 2017
[analyzer] Fix crash on modeling of pointer arithmetic
This patch fixes analyzer's crash on the newly added test case (see also https://bugs.llvm.org/show_bug.cgi?id=34374). Pointers subtraction appears to be modeled incorrectly in the following example: char* p; auto n = p - reinterpret_cast<char*>((unsigned long)1); In this case the analyzer (built without this patch) tries to create a symbolic value for the difference treating reinterpret_cast<char*>((unsigned long)1) as an integer, that is not correct. Differential revision: https://reviews.llvm.org/D38214 Test plan: make check-all git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@314141 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 7c4fc66 commit 31d5dae

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed
 

‎lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -726,9 +726,11 @@ SVal SimpleSValBuilder::evalBinOpLL(ProgramStateRef state,
726726
if (Optional<loc::ConcreteInt> rInt = rhs.getAs<loc::ConcreteInt>()) {
727727
// If one of the operands is a symbol and the other is a constant,
728728
// build an expression for use by the constraint manager.
729-
if (SymbolRef lSym = lhs.getAsLocSymbol(true))
730-
return MakeSymIntVal(lSym, op, rInt->getValue(), resultTy);
731-
729+
if (SymbolRef lSym = lhs.getAsLocSymbol(true)) {
730+
if (BinaryOperator::isComparisonOp(op))
731+
return MakeSymIntVal(lSym, op, rInt->getValue(), resultTy);
732+
return UnknownVal();
733+
}
732734
// Special case comparisons to NULL.
733735
// This must come after the test if the LHS is a symbol, which is used to
734736
// build constraints. The address of any non-symbolic region is guaranteed

‎test/Analysis/ptr-arith.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,9 @@ bool ptrAsIntegerSubtractionNoCrash(__UINTPTR_TYPE__ x, char *p) {
111111
__UINTPTR_TYPE__ y = (__UINTPTR_TYPE__)p - 1;
112112
return y == x;
113113
}
114+
115+
// Bug 34374
116+
bool integerAsPtrSubtractionNoCrash(char *p, __UINTPTR_TYPE__ m) {
117+
auto n = p - reinterpret_cast<char*>((__UINTPTR_TYPE__)1);
118+
return n == m;
119+
}

0 commit comments

Comments
 (0)