Hi,

a missing check in gfc_set_array_spec for for sum of rank and corank
not exceeding GFC_MAX_DIMENSIONS could trigger an assert on invalid code.

The attached patch fixes that.  OK for mainline / backport to 9-branch?

Harald


2020-04-19  Harald Anlauf  <anl...@gmx.de>

        PR fortran/93364
        * array.c (gfc_set_array_spec): Check for sum of rank and corank
        not exceeding GFC_MAX_DIMENSIONS.


2020-04-19  Harald Anlauf  <anl...@gmx.de>

        PR fortran/93364
        * gfortran.dg/pr93364.f90: New test.


diff --git a/gcc/fortran/array.c b/gcc/fortran/array.c
index 57972bc9176..471523fb767 100644
--- a/gcc/fortran/array.c
+++ b/gcc/fortran/array.c
@@ -864,6 +864,10 @@ gfc_set_array_spec (gfc_symbol *sym, gfc_array_spec *as, 
locus *error_loc)
       return false;
     }

+  /* Check F2018:C822.  */
+  if (sym->as->rank + sym->as->corank > GFC_MAX_DIMENSIONS)
+    goto too_many;
+
   if (as->corank)
     {
       sym->as->cotype = as->cotype;
diff --git a/gcc/testsuite/gfortran.dg/pr93364.f90 
b/gcc/testsuite/gfortran.dg/pr93364.f90
new file mode 100644
index 00000000000..61d7fa149a6
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr93364.f90
@@ -0,0 +1,13 @@
+! { dg-do compile }
+! { dg-options "-fcoarray=single" }
+!
+! PR fortran/93364 - check fix for ICE in gfc_set_array_spec
+
+type(t) function f()
+  codimension :: t[1,2,1,2,1,2,1,*]
+  dimension :: t(1,2,1,2,1,2,1,2)
+end
+
+! { dg-error "has not been declared" " " { target *-*-* } 6 }
+! { dg-error "is of type 't'" " " { target *-*-* } 6 }
+! { dg-error "rank \\+ corank of" " " { target *-*-* } 8 }

Reply via email to