A character variable appearing as a data statement object cannot
be automatic, thus it shall have constant length.
gcc/fortran/ChangeLog:
PR fortran/99205
* data.c (gfc_assign_data_value): Reject non-constant character
length for lvalue.
* trans-array.c (gfc_conv_array_initializer): Restrict loop to
elements which are defined to avoid NULL pointer dereference.
gcc/testsuite/ChangeLog:
PR fortran/99205
* gfortran.dg/data_char_4.f90: New test.
* gfortran.dg/data_char_5.f90: New test.
/* An initializer has to be constant. */
if (lvalue->ts.u.cl->length == NULL && !(ref && ref->u.ss.length != NULL))
return false;
+ if (lvalue->ts.u.cl->length
+ && lvalue->ts.u.cl->length->expr_type != EXPR_CONSTANT)
+ return false;
expr = create_character_initializer (init, last_ts, ref, rvalue);
if (!expr)
return false;
case EXPR_ARRAY:
/* Create a vector of all the elements. */
for (c = gfc_constructor_first (expr->value.constructor);
- c; c = gfc_constructor_next (c))
+ c && c->expr; c = gfc_constructor_next (c))
{
if (c->iterator)
{
--- /dev/null
+! { dg-do compile }
+! PR fortran/99205 - Out of memory with undefined character length
+! { dg-options "-w" }
+
+program p
+ character(l) :: c(2) ! { dg-error "must have constant character length" }
+ data c /'a', 'b'/
+ common c
+end
+
+! { dg-error "cannot appear in the expression at" " " { target *-*-* } 6 }
--- /dev/null
+! { dg-do compile }
+! PR fortran/99205 - Issues with non-constant character length
+
+subroutine sub ()
+ integer :: ll = 4
+ block
+ character(ll) :: c(2) ! { dg-error "non-constant" }
+ data c /'a', 'b'/
+ end block
+contains
+ subroutine sub1 ()
+ character(ll) :: d(2) ! { dg-error "non-constant" }
+ data d /'a', 'b'/
+ end subroutine sub1
+end subroutine sub