This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [Patch] Fix infinite loop/crash if array initializer index equals max value
- From: Senthil Kumar Selvaraj <senthil_kumar dot selvaraj at atmel dot com>
- To: "Joseph S. Myers" <joseph at codesourcery dot com>
- Cc: "gcc-patches at gcc dot gnu dot org" <gcc-patches at gcc dot gnu dot org>, "joern dot rennecke at embecosm dot com" <joern dot rennecke at embecosm dot com>
- Date: Wed, 4 Sep 2013 19:04:19 +0530
- Subject: Re: [Patch] Fix infinite loop/crash if array initializer index equals max value
- Authentication-results: sourceware.org; auth=none
- References: <32F9C30D650DFD43AADDA3FCB17101806AC7E8FA at penmbx02> <Pine dot LNX dot 4 dot 64 dot 1308232149070 dot 12585 at digraph dot polyomino dot org dot uk>
On Fri, Aug 23, 2013 at 09:49:55PM +0000, Joseph S. Myers wrote:
> On Thu, 22 Aug 2013, Selvaraj, Senthil_Kumar wrote:
>
> > 2013-08-23 Senthil Kumar Selvaraj <senthil_kumar.selvaraj@atmel.com>
> > * c-typeck.c (output_pending_init_elements): Handle overflow of
> > constructor_unfilled_index.
>
> This patch needs to add include a testcase to the testsuite that fails
> before and passes after the patch. (I realise such a test may only be
> able to run for a subset of targets.)
Reattaching the patch with a testcase for the AVR target. I'm not sure
how to generalize the testcase for other targets - the constant is the
max value (unsigned) of the mode used to represent initialized array
indices.
The attached test fails with a timeout before applying the patch, and
passes after applying it.
Regards
Senthil
gcc/c/ChangeLog
2013-09-04 Senthil Kumar Selvaraj <senthil_kumar.selvaraj@atmel.com>
* c-typeck.c (output_pending_init_elements): Handle overflow of
constructor_unfilled_index.
gcc/testsuite/ChangeLog
2013-09-04 Senthil Kumar Selvaraj <senthil_kumar.selvaraj@atmel.com>
* gcc.dg/large-size-array-7.c: New test to verify overflow handling
of constructor_unfilled_index.
diff --git gcc/c/c-typeck.c gcc/c/c-typeck.c
index 30871db..ed2e37a 100644
--- gcc/c/c-typeck.c
+++ gcc/c/c-typeck.c
@@ -7953,8 +7953,9 @@ output_pending_init_elements (int all, struct obstack * braced_init_obstack)
TREE_TYPE (constructor_type),
constructor_unfilled_index, 0, false,
braced_init_obstack);
- else if (tree_int_cst_lt (constructor_unfilled_index,
- elt->purpose))
+ else if (!TREE_OVERFLOW_P (constructor_unfilled_index)
+ && tree_int_cst_lt (constructor_unfilled_index,
+ elt->purpose))
{
/* Advance to the next smaller node. */
if (elt->left)
@@ -7979,7 +7980,8 @@ output_pending_init_elements (int all, struct obstack * braced_init_obstack)
while (elt->parent && elt->parent->right == elt)
elt = elt->parent;
elt = elt->parent;
- if (elt && tree_int_cst_lt (constructor_unfilled_index,
+ if (elt && !TREE_OVERFLOW_P (constructor_unfilled_index)
+ && tree_int_cst_lt (constructor_unfilled_index,
elt->purpose))
{
next = elt->purpose;
diff --git gcc/testsuite/gcc.dg/large-size-array-7.c gcc/testsuite/gcc.dg/large-size-array-7.c
new file mode 100644
index 0000000..196767d
--- /dev/null
+++ gcc/testsuite/gcc.dg/large-size-array-7.c
@@ -0,0 +1,5 @@
+/* { dg-do compile { target "avr-*-*" } } */
+/* { dg-options "-O2" } */
+static char * name[] = {
+ [0xFFFFFF] = "bar"
+ }; /* { dg-error "too large" } */