This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Don't issue array bound warnings on zero-length arrays


Hi All,

This patch fixes a minor issue that can occur when issuing array bounds
warnings.  In GNU C mode we allow empty lists and their upper bound is
initialized to -1.  This confuses the array bound analysis in VRP and
in some cases we end up issuing false positives.  This patch fixes
the issue by bailing out when a zero-length array is encountered.

OK for trunk?

gcc/

2013-08-30  Meador Inge  <meadori@codesourcery.com>

	* tree-vrp.c (check_array_ref): Bail out no emtpy arrays.

gcc/testsuite/

2013-08-30  Meador Inge  <meadori@codesourcery.com>

	* gcc.dg/Warray-bounds-11.c: New testcase.

Index: gcc/testsuite/gcc.dg/Warray-bounds-11.c
===================================================================
--- gcc/testsuite/gcc.dg/Warray-bounds-11.c	(revision 0)
+++ gcc/testsuite/gcc.dg/Warray-bounds-11.c	(revision 0)
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -Warray-bounds -std=gnu99" } */
+/* Test zero-length arrays for GNU C.  */
+
+unsigned int a[] = { };
+unsigned int size_a;
+
+int test(void)
+{
+  /* This should not warn.  */
+  return size_a ? a[0] : 0;
+}
Index: gcc/tree-vrp.c
===================================================================
--- gcc/tree-vrp.c	(revision 202088)
+++ gcc/tree-vrp.c	(working copy)
@@ -6137,9 +6137,10 @@ check_array_ref (location_t location, tr
   low_sub = up_sub = TREE_OPERAND (ref, 1);
   up_bound = array_ref_up_bound (ref);
 
-  /* Can not check flexible arrays.  */
+  /* Can not check flexible arrays or zero-length arrays.  */
   if (!up_bound
-      || TREE_CODE (up_bound) != INTEGER_CST)
+      || TREE_CODE (up_bound) != INTEGER_CST
+      || tree_int_cst_equal (up_bound, integer_minus_one_node))
     return;
 
   /* Accesses to trailing arrays via pointers may access storage


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]