]> granicus.if.org Git - clang/commitdiff
[analyzer] A fix for substraction of an integer from a pointer.
authorGabor Horvath <xazax.hun@gmail.com>
Fri, 18 Sep 2015 19:13:22 +0000 (19:13 +0000)
committerGabor Horvath <xazax.hun@gmail.com>
Fri, 18 Sep 2015 19:13:22 +0000 (19:13 +0000)
Patch by Artem Dergachev!

Differential Revision: http://reviews.llvm.org/D12725

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@248021 91177308-0d34-0410-b5e6-96231b3b80d8

lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp
test/Analysis/ptr-arith.c

index 6989526a561ff169803adbcd7ded5c5193f15596..a704ce224554fc4e1c78ee1c2b582167ce842c52 100644 (file)
@@ -911,8 +911,9 @@ SVal SimpleSValBuilder::evalBinOpLN(ProgramStateRef state,
       elementType = elemReg->getElementType();
     }
     else if (isa<SubRegion>(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();
     }
index 96dc8bacbce3df0774743aae4d95b943d1d39017..57463cc7c871db02f58736cb6a05a2c46b9b0191 100644 (file)
@@ -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}}
+}
+