From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 1256 invoked by alias); 18 Oct 2002 17:26:03 -0000 Mailing-List: contact gcc-prs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-prs-owner@gcc.gnu.org Received: (qmail 1240 invoked by uid 71); 18 Oct 2002 17:26:03 -0000 Date: Fri, 18 Oct 2002 10:26:00 -0000 Message-ID: <20021018172603.1238.qmail@sources.redhat.com> To: nobody@gcc.gnu.org Cc: gcc-prs@gcc.gnu.org, From: Falk Hueffner Subject: Re: c/8268: no compile time array index checking Reply-To: Falk Hueffner X-SW-Source: 2002-10/txt/msg00717.txt.bz2 List-Id: The following reply was made to PR c/8268; it has been noted by GNATS. From: Falk Hueffner To: gcc-gnats@gcc.gnu.org Cc: Subject: Re: c/8268: no compile time array index checking Date: 18 Oct 2002 19:22:53 +0200 --=-=-= 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 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=array-bounds.patch 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 --=-=-=--