]> granicus.if.org Git - clang/commitdiff
Fix a couple bugs in the way we handle array indexes in array bounds checking. Speci...
authorEli Friedman <eli.friedman@gmail.com>
Mon, 27 Feb 2012 21:21:40 +0000 (21:21 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Mon, 27 Feb 2012 21:21:40 +0000 (21:21 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@151569 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaChecking.cpp
test/SemaCXX/array-bounds.cpp

index e96306535a3bf10d2bd665bfb4f039eda7db18c7..3d9f5b3afd2c8c89df75671c9b14eec3517c65f9 100644 (file)
@@ -4440,7 +4440,7 @@ static bool IsTailPaddedMemberArray(Sema &S, llvm::APInt Size,
 void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
                             const ArraySubscriptExpr *ASE,
                             bool AllowOnePastEnd, bool IndexNegated) {
-  IndexExpr = IndexExpr->IgnoreParenCasts();
+  IndexExpr = IndexExpr->IgnoreParenImpCasts();
   if (IndexExpr->isValueDependent())
     return;
 
@@ -4486,15 +4486,15 @@ void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
     }
 
     if (size.getBitWidth() > index.getBitWidth())
-      index = index.sext(size.getBitWidth());
+      index = index.zext(size.getBitWidth());
     else if (size.getBitWidth() < index.getBitWidth())
-      size = size.sext(index.getBitWidth());
+      size = size.zext(index.getBitWidth());
 
     // For array subscripting the index must be less than size, but for pointer
     // arithmetic also allow the index (offset) to be equal to size since
     // computing the next address after the end of the array is legal and
     // commonly done e.g. in C++ iterators and range-based for loops.
-    if (AllowOnePastEnd ? index.sle(size) : index.slt(size))
+    if (AllowOnePastEnd ? index.ule(size) : index.ult(size))
       return;
 
     // Also don't warn for arrays of size 1 which are members of some
index c1b37011727da8a70fffb2b5ca8d7a86d88a7861..57a9e3de6a298e114ad55a5a4203c3b4ea8b90b9 100644 (file)
@@ -247,3 +247,9 @@ void test_pr11007() {
   double a[5]; // expected-note {{array 'a' declared here}}
   test_pr11007_aux("foo", a[1000]); // expected-warning {{array index 1000 is past the end of the array}}
 }
+
+void test_rdar10916006(void)
+{
+       int a[128]; // expected-note {{array 'a' declared here}}
+       a[(unsigned char)'\xA1'] = 1; // expected-warning {{array index 161 is past the end of the array}}
+}