PR fortran/99205 - Out of memory with undefined character length
authorHarald Anlauf <anlauf@gmx.de>
Wed, 10 Mar 2021 21:59:50 +0000 (22:59 +0100)
committerHarald Anlauf <anlauf@gmx.de>
Wed, 10 Mar 2021 21:59:50 +0000 (22:59 +0100)
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
gcc/fortran/trans-array.c
gcc/testsuite/gfortran.dg/data_char_4.f90 [new file with mode: 0644]
gcc/testsuite/gfortran.dg/data_char_5.f90 [new file with mode: 0644]

index 25e9793..71e2552 100644 (file)
@@ -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;
index c672565..478cddd 100644 (file)
@@ -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 (file)
index 0000000..ed0782c
--- /dev/null
@@ -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 (file)
index 0000000..ea26687
--- /dev/null
@@ -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