From: Harald Anlauf Date: Wed, 10 Mar 2021 21:59:50 +0000 (+0100) Subject: PR fortran/99205 - Out of memory with undefined character length X-Git-Tag: upstream/12.2.0~9340 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8c21bc6646dbe3365d7f89843a79eee823aa3b52;p=platform%2Fupstream%2Fgcc.git PR fortran/99205 - Out of memory with undefined character length 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. --- diff --git a/gcc/fortran/data.c b/gcc/fortran/data.c index 25e9793..71e2552 100644 --- a/gcc/fortran/data.c +++ b/gcc/fortran/data.c @@ -595,6 +595,9 @@ gfc_assign_data_value (gfc_expr *lvalue, gfc_expr *rvalue, mpz_t index, /* 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; diff --git a/gcc/fortran/trans-array.c b/gcc/fortran/trans-array.c index c672565..478cddd 100644 --- a/gcc/fortran/trans-array.c +++ b/gcc/fortran/trans-array.c @@ -6162,7 +6162,7 @@ gfc_conv_array_initializer (tree type, gfc_expr * expr) 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) { diff --git a/gcc/testsuite/gfortran.dg/data_char_4.f90 b/gcc/testsuite/gfortran.dg/data_char_4.f90 new file mode 100644 index 0000000..ed0782c --- /dev/null +++ b/gcc/testsuite/gfortran.dg/data_char_4.f90 @@ -0,0 +1,11 @@ +! { 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 } diff --git a/gcc/testsuite/gfortran.dg/data_char_5.f90 b/gcc/testsuite/gfortran.dg/data_char_5.f90 new file mode 100644 index 0000000..ea26687 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/data_char_5.f90 @@ -0,0 +1,15 @@ +! { 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