This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: RFA: Fix c/57821
- From: Joern Rennecke <joern dot rennecke at embecosm dot com>
- To: "Joseph S. Myers" <joseph at codesourcery dot com>
- Cc: gcc-patches at gcc dot gnu dot org, Richard Biener <rguenther at suse dot de>
- Date: Thu, 04 Jul 2013 16:28:42 -0400
- Subject: Re: RFA: Fix c/57821
- References: <20130704150020 dot lv606jwfogcckk0c-nzlynne at webmail dot spamcop dot net> <Pine dot LNX dot 4 dot 64 dot 1307041919360 dot 15783 at digraph dot polyomino dot org dot uk>
Quoting "Joseph S. Myers" <joseph@codesourcery.com>:
On Thu, 4 Jul 2013, Joern Rennecke wrote:
For this fix I've assumed that it is now the job of the various places that
do calculations with sizetypes to flag overflows where this is desired.
Integer constants may be shared, I don't think setting TREE_OVERFLOW on
existing constants like you do is necessarily safe.
Hmm, indeed. I now added a copy_node call in set_init_index to avoid this
problem. For complete_array_type, we need this information just a bit
further thon in the function, so I just made it a boolean flag.
Also note c/ and c-family/ have their own ChangeLog files.
Oops. I though I checked that. Foiled by ls localisation.
I'm currently testing the attached updated patch.
2013-07-04 Joern Rennecke <joern.rennecke@embecosm.com>
gcc/c:
PR c/57821
* c-typeck.c (set_init_index): When folding, check for index overflow.
gcc/c-family:
PR c/57821
* c-common.c (complete_array_type): Delay folding first index
like other indices. When folding, check for index overflow.
gcc/testsuite:
PR c/57821
* gcc.dg/large-size-array-6.c: New test.
Index: c/c-typeck.c
===================================================================
--- c/c-typeck.c (revision 200606)
+++ c/c-typeck.c (working copy)
@@ -7217,6 +7217,11 @@ set_init_index (tree first, tree last,
if (last)
constant_expression_warning (last);
constructor_index = convert (bitsizetype, first);
+ if (tree_int_cst_lt (constructor_index, first))
+ {
+ constructor_index = copy_node (constructor_index);
+ TREE_OVERFLOW (constructor_index) = 1;
+ }
if (last)
{
Index: c-family/c-common.c
===================================================================
--- c-family/c-common.c (revision 200606)
+++ c-family/c-common.c (working copy)
@@ -9781,6 +9781,7 @@ complete_array_type (tree *ptype, tree i
tree maxindex, type, main_type, elt, unqual_elt;
int failure = 0, quals;
hashval_t hashcode = 0;
+ bool overflow_p = false;
maxindex = size_zero_node;
if (initial_value)
@@ -9809,8 +9810,8 @@ complete_array_type (tree *ptype, tree i
bool fold_p = false;
if ((*v)[0].index)
- maxindex = fold_convert_loc (input_location, sizetype,
- (*v)[0].index);
+ maxindex = (*v)[0].index, fold_p = true;
+
curindex = maxindex;
for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
@@ -9821,15 +9822,26 @@ complete_array_type (tree *ptype, tree i
else
{
if (fold_p)
- curindex = fold_convert (sizetype, curindex);
+ {
+ /* Since we treat size types now as ordinary
+ unsigned types, we need an explicit overflow
+ check. */
+ tree orig = curindex;
+ curindex = fold_convert (sizetype, curindex);
+ overflow_p |= tree_int_cst_lt (curindex, orig);
+ }
curindex = size_binop (PLUS_EXPR, curindex,
size_one_node);
}
if (tree_int_cst_lt (maxindex, curindex))
maxindex = curindex, fold_p = curfold_p;
}
- if (fold_p)
- maxindex = fold_convert (sizetype, maxindex);
+ if (fold_p)
+ {
+ tree orig = maxindex;
+ maxindex = fold_convert (sizetype, maxindex);
+ overflow_p |= tree_int_cst_lt (maxindex, orig);
+ }
}
}
else
@@ -9890,7 +9902,7 @@ complete_array_type (tree *ptype, tree i
if (COMPLETE_TYPE_P (type)
&& TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
- && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
+ && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
{
error ("size of array is too large");
/* If we proceed with the array type as it is, we'll eventually
Index: testsuite/gcc.dg/large-size-array-6.c
===================================================================
--- testsuite/gcc.dg/large-size-array-6.c (revision 0)
+++ testsuite/gcc.dg/large-size-array-6.c (working copy)
@@ -0,0 +1,6 @@
+/* PR c/57821 */
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+static char * name[] = {
+ [0x8000000000000000] = "bar"
+ }; /* { dg-error "too large" } */