From 8c21bc6646dbe3365d7f89843a79eee823aa3b52 Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Wed, 10 Mar 2021 22:59:50 +0100 Subject: [PATCH] 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. --- gcc/fortran/data.c | 3 +++ gcc/fortran/trans-array.c | 2 +- gcc/testsuite/gfortran.dg/data_char_4.f90 | 11 +++++++++++ gcc/testsuite/gfortran.dg/data_char_5.f90 | 15 +++++++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gfortran.dg/data_char_4.f90 create mode 100644 gcc/testsuite/gfortran.dg/data_char_5.f90 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 -- 2.7.4