public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] fix off-by-one mistake in -Warray-bounds for major bounds (PR 84079)
@ 2020-07-22 23:35 Martin Sebor
  0 siblings, 0 replies; only message in thread
From: Martin Sebor @ 2020-07-22 23:35 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 339 bytes --]

-Warray-bounds fails to trigger when taking the address of an element
of a multi-dimensional array at an index that's equal to the bound of
one of the higher dimensions of the array.  The attached simple patch
corrects this shortcoming.  I will commit it tomorrow unless there are
suggestions for changes.

Tested on x86_64-linux.

Martin

[-- Attachment #2: gcc-84079.diff --]
[-- Type: text/x-patch, Size: 6627 bytes --]

PR tree-optimization/84079 - missing -Warray-bounds taking the address of past-the-end element of a multidimensional array

gcc/ChangeLog:

	PR tree-optimization/84079
	* gimple-array-bounds.cc (array_bounds_checker::check_addr_expr):

gcc/testsuite/ChangeLog:

	PR tree-optimization/84079
	* gcc.dg/Warray-bounds-62.c: New test.


diff --git a/gcc/gimple-array-bounds.cc b/gcc/gimple-array-bounds.cc
index 352d0745178..613bd612139 100644
--- a/gcc/gimple-array-bounds.cc
+++ b/gcc/gimple-array-bounds.cc
@@ -519,14 +519,21 @@ array_bounds_checker::check_mem_ref (location_t location, tree ref,
 void
 array_bounds_checker::check_addr_expr (location_t location, tree t)
 {
+  /* For the rightmost subscript only, accept taking the address of
+     the just-past-the-end element.  */
+  bool ignore_off_by_one = true;
+
   /* Check each ARRAY_REF and MEM_REF in the reference chain. */
   do
     {
       bool warned = false;
       if (TREE_CODE (t) == ARRAY_REF)
-	warned = check_array_ref (location, t, true /*ignore_off_by_one*/);
+	{
+	  warned = check_array_ref (location, t, ignore_off_by_one);
+	  ignore_off_by_one = false;
+	}
       else if (TREE_CODE (t) == MEM_REF)
-	warned = check_mem_ref (location, t, true /*ignore_off_by_one*/);
+	warned = check_mem_ref (location, t, ignore_off_by_one);
 
       if (warned)
 	TREE_NO_WARNING (t) = true;
diff --git a/gcc/testsuite/gcc.dg/Warray-bounds-62.c b/gcc/testsuite/gcc.dg/Warray-bounds-62.c
new file mode 100644
index 00000000000..c2421aac1b2
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Warray-bounds-62.c
@@ -0,0 +1,130 @@
+/* PR tree-optimization/84079 - missing -Warray-bounds taking the address
+   of past-the-end element of a multidimensional array
+   { dg-do compile }
+   { dg-options "-O2 -Wall -ftrack-macro-expansion=0" } */
+
+void sink (int, ...);
+
+#define T(type, dims, inxs)			\
+  do {						\
+    type a dims;				\
+    sink (__LINE__, &a inxs);			\
+  } while (0)
+
+
+void test_char_1_1 (int i0, int i1, int i2)
+{
+#undef DIMS
+#define DIMS [1][1]
+
+  T (char, DIMS, [0]);
+  T (char, DIMS, [1]);
+  T (char, DIMS, [2]);            // { dg-warning "subscript 2 is above array bounds of 'char\\\[1]\\\[1]'" }
+
+  T (char, DIMS, [0][0]);
+  T (char, DIMS, [0][1]);
+  T (char, DIMS, [0][2]);         // { dg-warning "subscript 2 is above array bounds of 'char\\\[1]'" }
+
+  T (char, DIMS, [1][0]);         // { dg-warning "subscript 1 is above array bounds of 'char\\\[1]\\\[1]'" }
+  T (char, DIMS, [1][1]);         // { dg-warning "subscript 1 is above array bounds of 'char\\\[1]\\\[1]'" }
+  T (char, DIMS, [1][2]);         // { dg-warning "subscript 2 is above array bounds of 'char\\\[1]'" }
+
+  // Exercise ranges.
+  if (i0 < 0) i0 = 0;
+  if (i1 < 1) i1 = 1;
+  if (i2 < 2) i2 = 2;
+
+  T (char, DIMS, [i0]);
+  T (char, DIMS, [i1]);
+  T (char, DIMS, [i2]);           // { dg-warning "subscript 2 is above array bounds of 'char\\\[1]\\\[1]" }
+
+  T (char, DIMS, [i0][i0]);
+  T (char, DIMS, [i0][i1]);
+  T (char, DIMS, [i1][i0]);       // { dg-warning "subscript 1 is above array bounds of 'char\\\[1]\\\[1]'" }
+  T (char, DIMS, [i1][i1]);       // { dg-warning "subscript 1 is above array bounds of 'char\\\[1]\\\[1]'" }
+  T (char, DIMS, [i1][i2]);       // { dg-warning "subscript 2 is above array bounds of 'char\\\[1]'" }
+}
+
+
+void test_int_3_5 (int i0, int i1, int i2, int i3, int i4, int i5, int i6)
+{
+#undef DIMS
+#define DIMS [3][5]
+
+  T (int, DIMS, [0]);
+  T (int, DIMS, [3]);
+  T (int, DIMS, [4]);             // { dg-warning "subscript 4 is above array bounds of 'int\\\[3]\\\[5]'" }
+
+  T (int, DIMS, [0][0]);
+  T (int, DIMS, [0][5]);
+  T (int, DIMS, [0][6]);          // { dg-warning "subscript 6 is above array bounds of 'int\\\[5]'" }
+
+  T (int, DIMS, [1][0]);
+  T (int, DIMS, [1][5]);
+  T (int, DIMS, [1][6]);          // { dg-warning "subscript 6 is above array bounds of 'int\\\[5]'" }
+
+  T (int, DIMS, [3][0]);          // { dg-warning "subscript 3 is above array bounds of 'int\\\[3]\\\[5]'" }
+  T (int, DIMS, [3][5]);          // { dg-warning "subscript 3 is above array bounds of 'int\\\[3]\\\[5]'" }
+  T (int, DIMS, [3][6]);          // { dg-warning "subscript 6 is above array bounds of 'int\\\[5]'" }
+
+  // Exercise ranges.
+  if (i0 < 0) i0 = 0;
+  if (i1 < 1) i1 = 1;
+  if (i2 < 2) i2 = 2;
+  if (i3 < 3) i3 = 3;
+  if (i4 < 4) i4 = 4;
+  if (i5 < 5) i5 = 5;
+  if (i6 < 6) i6 = 6;
+
+  T (int, DIMS, [i0]);
+  T (int, DIMS, [i3]);
+  T (int, DIMS, [i4]);            // { dg-warning "subscript 4 is above array bounds of 'int\\\[3]\\\[5]" }
+
+  T (int, DIMS, [i0][i0]);
+  T (int, DIMS, [i0][i5]);
+  T (int, DIMS, [i0][i6]);        // { dg-warning "subscript 6 is above array bounds of 'int\\\[5]'" }
+
+  T (int, DIMS, [i1][i0]);
+  T (int, DIMS, [i1][i5]);
+  T (int, DIMS, [i1][i6]);        // { dg-warning "subscript 6 is above array bounds of 'int\\\[5]'" }
+
+  T (int, DIMS, [i3][i0]);        // { dg-warning "subscript 3 is above array bounds of 'int\\\[3]\\\[5]'" }
+  T (int, DIMS, [i3][i5]);        // { dg-warning "subscript 3 is above array bounds of 'int\\\[3]\\\[5]'" }
+  T (int, DIMS, [i3][i6]);        // { dg-warning "subscript 6 is above array bounds of 'int\\\[5]'" }
+}
+
+
+void test_int_2_3_4_5 (void)
+{
+#undef DIMS
+#define DIMS [2][3][4][5]
+
+  T (int, DIMS, [0]);
+  T (int, DIMS, [2]);
+  T (int, DIMS, [3]);             // { dg-warning "subscript 3 is above array bounds of 'int\\\[2]\\\[3]\\\[4]\\\[5]'" }
+
+  T (int, DIMS, [0][0]);
+  T (int, DIMS, [0][3]);
+  T (int, DIMS, [0][4]);          // { dg-warning "subscript 4 is above array bounds of 'int\\\[3]\\\[4]\\\[5]'" }
+  T (int, DIMS, [0][9]);          // { dg-warning "subscript 9 is above array bounds of 'int\\\[3]\\\[4]\\\[5]'" }
+
+  T (int, DIMS, [0][0][0]);
+  T (int, DIMS, [0][0][4]);
+  T (int, DIMS, [0][0][5]);       // { dg-warning "subscript 5 is above array bounds of 'int\\\[4]\\\[5]'" }
+
+  T (int, DIMS, [0][0][0][0]);
+  T (int, DIMS, [0][0][0][5]);
+  T (int, DIMS, [0][0][0][6]);    // { dg-warning "subscript 6 is above array bounds of 'int\\\[5]'" }
+
+  T (int, DIMS, [0][0][1][0]);
+  T (int, DIMS, [0][0][1][5]);
+  T (int, DIMS, [0][0][1][6]);    // { dg-warning "subscript 6 is above array bounds of 'int\\\[5]'" }
+
+  T (int, DIMS, [0][0][3][0]);
+  T (int, DIMS, [0][0][3][5]);
+  T (int, DIMS, [0][0][3][6]);    // { dg-warning "subscript 6 is above array bounds of 'int\\\[5]'" }
+
+  T (int, DIMS, [0][0][1][0]);
+  T (int, DIMS, [0][0][1][5]);
+  T (int, DIMS, [0][0][1][6]);    // { dg-warning "subscript 6 is above array bounds of 'int\\\[5]'" }
+}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-07-22 23:35 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-22 23:35 [PATCH] fix off-by-one mistake in -Warray-bounds for major bounds (PR 84079) Martin Sebor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).