From: Gabriel Dos Reis Date: Wed, 21 Jan 2004 08:06:38 +0000 (+0000) Subject: backport: re PR c++/13574 (array default initializer in class lets gcc consume all... X-Git-Tag: releases/gcc-3.3.3~70 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=38d586ff58c9cdb51e446107d87fdf6f5c48ab90;p=thirdparty%2Fgcc.git backport: re PR c++/13574 (array default initializer in class lets gcc consume all memory and die) Backport from mainline 2004-01-16 Mark Mitchell PR c++/13574 * decl.c (compute_array_index_type): Fix grammar in comment. * init.c (build_zero_init): Handle zero-sized arrays correctly. From-SVN: r76265 --- diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index ece486368b1a..225c02d5cf43 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,12 @@ +2004-01-21 Gabriel Dos Reis + + Backport from mainline + 2004-01-16 Mark Mitchell + + PR c++/13574 + * decl.c (compute_array_index_type): Fix grammar in comment. + * init.c (build_zero_init): Handle zero-sized arrays correctly. + 2004-01-21 Gabriel Dos Reis Backport from mainline diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index a70e9941a040..6dda5dbbd311 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -10176,9 +10176,8 @@ compute_array_index_type (name, size) error ("size of array is negative"); size = integer_one_node; } - /* Except that an extension we allow zero-sized arrays. We - always allow them in system headers because glibc uses - them. */ + /* As an extension we allow zero-sized arrays. We always allow + them in system headers because glibc uses them. */ else if (integer_zerop (size) && pedantic && !in_system_header) { if (name) diff --git a/gcc/cp/init.c b/gcc/cp/init.c index 5457ac56a1a2..ba30a9b61774 100644 --- a/gcc/cp/init.c +++ b/gcc/cp/init.c @@ -245,15 +245,18 @@ build_zero_init (tree type, tree nelts, bool static_storage_p) max_index = nelts ? nelts : array_type_nelts (type); my_friendly_assert (TREE_CODE (max_index) == INTEGER_CST, 20030618); - for (index = size_zero_node; - !tree_int_cst_lt (max_index, index); - index = size_binop (PLUS_EXPR, index, size_one_node)) - inits = tree_cons (index, - build_zero_init (TREE_TYPE (type), - /*nelts=*/NULL_TREE, - static_storage_p), - inits); - CONSTRUCTOR_ELTS (init) = nreverse (inits); + /* A zero-sized array, which is accepted as an extension, will + have an upper bound of -1. */ + if (!tree_int_cst_equal (max_index, integer_minus_one_node)) + for (index = size_zero_node; + !tree_int_cst_lt (max_index, index); + index = size_binop (PLUS_EXPR, index, size_one_node)) + inits = tree_cons (index, + build_zero_init (TREE_TYPE (type), + /*nelts=*/NULL_TREE, + static_storage_p), + inits); + CONSTRUCTOR_ELTS (init) = nreverse (inits); } else if (TREE_CODE (type) == REFERENCE_TYPE) ; diff --git a/gcc/testsuite/g++.dg/ext/array1.C b/gcc/testsuite/g++.dg/ext/array1.C index 7e54dc91939b..d37cf912adc0 100644 --- a/gcc/testsuite/g++.dg/ext/array1.C +++ b/gcc/testsuite/g++.dg/ext/array1.C @@ -1,14 +1,14 @@ // PR c++/13574 // { dg-options "" } -class A { -public: - A() : argc(0), argv() { }; -private: - int argc; - char* argv[]; -}; - -int main() { - A y; +class A { +public: + A() : argc(0), argv() { }; + private: + int argc; + char* argv[]; +}; + +int main() { + A y; }