]> granicus.if.org Git - clang/commitdiff
Let -Warray-bounds handle casted array types without false positives.
authorNico Weber <nicolasweber@gmx.de>
Sat, 17 Sep 2011 22:59:41 +0000 (22:59 +0000)
committerNico Weber <nicolasweber@gmx.de>
Sat, 17 Sep 2011 22:59:41 +0000 (22:59 +0000)
Fixes PR10771.

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

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

index 6e750c182b199e93fcb878f4c15108a303bcef28..aecc659485dfdca244a703a3e099f15f8ee6b32b 100644 (file)
@@ -3709,7 +3709,7 @@ void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
       return;
 
     const Type* BaseType = getElementType(BaseExpr);
-    if (!isSubscript && BaseType != EffectiveType) {
+    if (BaseType != EffectiveType) {
       // Make sure we're comparing apples to apples when comparing index to size
       uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType);
       uint64_t array_typesize = Context.getTypeSize(BaseType);
index 2bbb4776ada8b3162f276d827d94ed5902bd3015..e071bc7b5bdd514be5fca16082bfe58dbce087a1 100644 (file)
@@ -214,3 +214,15 @@ int test_more() {
   else
     return foo[5]; // expected-warning {{array index of '5' indexes past the end of an array (that contains 5 elements)}}
 }
+
+void test_pr10771() {
+    double foo[4096];  // expected-note {{array 'foo' declared here}}
+
+    ((char*)foo)[sizeof(foo) - 1] = '\0';  // no-warning
+    *(((char*)foo) + sizeof(foo) - 1) = '\0';  // no-warning
+
+    ((char*)foo)[sizeof(foo)] = '\0';  // expected-warning {{array index of '32768' indexes past the end of an array (that contains 32768 elements)}}
+
+    // TODO: This should probably warn, too.
+    *(((char*)foo) + sizeof(foo)) = '\0';  // no-warning
+}