diff --git a/clang/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp b/clang/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp index 6989526a561f..a704ce224554 100644 --- a/clang/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp +++ b/clang/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp @@ -911,8 +911,9 @@ SVal SimpleSValBuilder::evalBinOpLN(ProgramStateRef state, elementType = elemReg->getElementType(); } else if (isa(region)) { + assert(op == BO_Add || op == BO_Sub); + index = (op == BO_Add) ? rhs : evalMinus(rhs); superR = region; - index = rhs; if (resultTy->isAnyPointerType()) elementType = resultTy->getPointeeType(); } diff --git a/clang/test/Analysis/ptr-arith.c b/clang/test/Analysis/ptr-arith.c index 96dc8bacbce3..57463cc7c871 100644 --- a/clang/test/Analysis/ptr-arith.c +++ b/clang/test/Analysis/ptr-arith.c @@ -296,3 +296,20 @@ void symbolicFieldRegion(struct Point *points, int i, int j) { clang_analyzer_eval(&points[i].x < &points[i].y);// expected-warning{{TRUE}} } +void negativeIndex(char *str) { + *(str + 1) = 'a'; + clang_analyzer_eval(*(str + 1) == 'a'); // expected-warning{{TRUE}} + clang_analyzer_eval(*(str - 1) == 'a'); // expected-warning{{UNKNOWN}} + + char *ptr1 = str - 1; + clang_analyzer_eval(*ptr1 == 'a'); // expected-warning{{UNKNOWN}} + + char *ptr2 = str; + ptr2 -= 1; + clang_analyzer_eval(*ptr2 == 'a'); // expected-warning{{UNKNOWN}} + + char *ptr3 = str; + --ptr3; + clang_analyzer_eval(*ptr3 == 'a'); // expected-warning{{UNKNOWN}} +} +