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

Chris Lattner sabre at nondot.org
Tue Aug 2 14:44:23 PDT 2011


Author: lattner
Date: Tue Aug  2 16:44:23 2011
New Revision: 136724

URL: http://llvm.org/viewvc/llvm-project?rev=136724&view=rev
Log:
disable array bounds overflow warning for cases where an array 
has a single element.  This disables the warning in cases where
there is a clear bug, but this is really rare (who uses arrays
with one element?) and it also silences a large class of false
positive issues with C89 code that is using tail padding in structs.

A better version of this patch would detect when an array is in
a tail position in a struct, but at least patch fixes the huge
false positives that are hitting postgres and other code.


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=136724&r1=136723&r2=136724&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Tue Aug  2 16:44:23 2011
@@ -3491,7 +3491,9 @@
     else if (size.getBitWidth() < index.getBitWidth())
       size = size.sext(index.getBitWidth());
 
-    if (index.slt(size))
+    // Don't warn for valid indexes, or arrays of size 1 (which are often
+    // tail-allocated arrays that are emulating flexible arrays in C89 code).
+    if (index.slt(size) || size == 1)
       return;
 
     S.DiagRuntimeBehavior(E->getBase()->getLocStart(), BaseExpr,

Modified: cfe/trunk/test/SemaCXX/array-bounds.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/array-bounds.cpp?rev=136724&r1=136723&r2=136724&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/array-bounds.cpp (original)
+++ cfe/trunk/test/SemaCXX/array-bounds.cpp Tue Aug  2 16:44:23 2011
@@ -24,8 +24,8 @@
   int val = a[3]; // no warning for function argumnet
 }
 
-void f2(const int (&a)[1]) { // expected-note {{declared here}}
-  int val = a[3];  // expected-warning {{array index of '3' indexes past the end of an array (that contains 1 elements)}}
+void f2(const int (&a)[2]) { // expected-note {{declared here}}
+  int val = a[3];  // expected-warning {{array index of '3' indexes past the end of an array (that contains 2 elements)}}
 }
 
 void test() {
@@ -42,8 +42,8 @@
   u.c[3] = 1; // no warning
 
   const int const_subscript = 3;
-  int array[1]; // expected-note {{declared here}}
-  array[const_subscript] = 0;  // expected-warning {{array index of '3' indexes past the end of an array (that contains 1 elements)}}
+  int array[2]; // expected-note {{declared here}}
+  array[const_subscript] = 0;  // expected-warning {{array index of '3' indexes past the end of an array (that contains 2 elements)}}
 
   int *ptr;
   ptr[3] = 0; // no warning for pointer references
@@ -58,8 +58,8 @@
   const char str2[] = "foo"; // expected-note {{declared here}}
   char c2 = str2[5]; // expected-warning {{array index of '5' indexes past the end of an array (that contains 4 elements)}}
 
-  int (*array_ptr)[1];
-  (*array_ptr)[3] = 1; // expected-warning {{array index of '3' indexes past the end of an array (that contains 1 elements)}}
+  int (*array_ptr)[2];
+  (*array_ptr)[3] = 1; // expected-warning {{array index of '3' indexes past the end of an array (that contains 2 elements)}}
 }
 
 template <int I> struct S {
@@ -173,3 +173,14 @@
   }
   x[2] = 0; // no-warning
 }
+
+namespace tailpad {
+  struct foo {
+    int x;
+    char c[1];
+  };
+  
+  char bar(struct foo *F) {
+    return F->c[3];  // no warning, foo could have tail padding allocated.
+  }
+}





More information about the cfe-commits mailing list