[cfe-commits] r151569 - in /cfe/trunk: lib/Sema/SemaChecking.cpp test/SemaCXX/array-bounds.cpp

Eli Friedman eli.friedman at gmail.com
Mon Feb 27 13:21:40 PST 2012


Author: efriedma
Date: Mon Feb 27 15:21:40 2012
New Revision: 151569

URL: http://llvm.org/viewvc/llvm-project?rev=151569&view=rev
Log:
Fix a couple bugs in the way we handle array indexes in array bounds checking.  Specifically, make sure we don't ignore explicit casts in indexes, and make sure we use unsigned extension/comparisons on indexes.  Fixes <rdar://problem/10916006>.

Modified:
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/test/SemaCXX/array-bounds.cpp

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=151569&r1=151568&r2=151569&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Mon Feb 27 15:21:40 2012
@@ -4440,7 +4440,7 @@
 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 @@
     }
 
     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

Modified: cfe/trunk/test/SemaCXX/array-bounds.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/array-bounds.cpp?rev=151569&r1=151568&r2=151569&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/array-bounds.cpp (original)
+++ cfe/trunk/test/SemaCXX/array-bounds.cpp Mon Feb 27 15:21:40 2012
@@ -247,3 +247,9 @@
   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}}
+}





More information about the cfe-commits mailing list