This is the mail archive of the gcc-bugs@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]

Re: c/8268: no compile time array index checking


Hi,

ages ago, I wrote a patch for c-typeck.c that does this. Jeff Law
suggested to place it in expr.c, so other languages would catch it,
too. Here's a patch. Does it look like I'm on the right track?

-- 
	Falk

Index: expr.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/expr.c,v
retrieving revision 1.488
diff -u -r1.488 expr.c
--- expr.c	15 Oct 2002 20:09:32 -0000	1.488
+++ expr.c	18 Oct 2002 15:59:49 -0000
@@ -5634,6 +5634,19 @@
 	  tree low_bound = (domain ? TYPE_MIN_VALUE (domain) : 0);
 	  tree unit_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (array)));
 
+	  if (domain && TREE_CODE (index) == INTEGER_CST)
+	    {
+	      if ((TREE_CODE (low_bound) == INTEGER_CST
+		   && tree_int_cst_lt(index, low_bound))
+		  || (TREE_CODE (TYPE_MAX_VALUE (domain)) == INTEGER_CST
+		      && tree_int_cst_lt (TYPE_MAX_VALUE (domain), index)
+		      /* Accesses after the end of arrays of size 0 (gcc
+			 extension) and 1 are likely intentional. */
+		      && !tree_int_cst_lt (TYPE_MAX_VALUE (domain),
+					   build_int_2 (2, 0))))
+		warning ("array subscript out of range");
+	    }
+
 	  /* We assume all arrays have sizes that are a multiple of a byte.
 	     First subtract the lower bound, if any, in the type of the
 	     index, then convert to sizetype and multiply by the size of the

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