This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: c/8268: no compile time array index checking
- From: Falk Hueffner <falk dot hueffner at student dot uni-tuebingen dot de>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 19 Oct 2002 00:41:29 +0200
- Subject: Re: c/8268: no compile time array index checking
- References: <20021017212011.28259.qmail@sources.redhat.com><8765vzslua.fsf@student.uni-tuebingen.de>
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