PR fortran/96086 - ICE in gfc_match_select_rank, at fortran/match.c:6645
authorHarald Anlauf <anlauf@gmx.de>
Fri, 10 Jul 2020 19:00:13 +0000 (21:00 +0200)
committerHarald Anlauf <anlauf@gmx.de>
Fri, 10 Jul 2020 19:00:13 +0000 (21:00 +0200)
Handle NULL pointer dereference on SELECT RANK with an invalid
assumed-rank array declaration.

gcc/fortran/
PR fortran/96086
* match.c (gfc_match_select_rank): Catch NULL pointer
dereference.
* resolve.c (resolve_assoc_var): Catch NULL pointer dereference
that may occur after an illegal declaration.

gcc/fortran/match.c
gcc/fortran/resolve.c
gcc/testsuite/gfortran.dg/pr96086.f90 [new file with mode: 0644]

index 7d3711c55f9857854e3c1254793c09106af5b967..cb09c5f8ec535d8fedd14b3a5585350dc0a74e73 100644 (file)
@@ -6647,7 +6647,8 @@ gfc_match_select_rank (void)
       if (expr2->symtree)
        {
          sym2 = expr2->symtree->n.sym;
-         as = sym2->ts.type == BT_CLASS ? CLASS_DATA (sym2)->as : sym2->as;
+         as = (sym2->ts.type == BT_CLASS
+               && CLASS_DATA (sym2)) ? CLASS_DATA (sym2)->as : sym2->as;
        }
 
       if (expr2->expr_type != EXPR_VARIABLE
@@ -6659,7 +6660,7 @@ gfc_match_select_rank (void)
          goto cleanup;
        }
 
-      if (expr2->ts.type == BT_CLASS)
+      if (expr2->ts.type == BT_CLASS && CLASS_DATA (sym2))
        {
          copy_ts_from_selector_to_associate (expr1, expr2);
 
index 6bc1c46a97dcbec28ca5f308a968ce7dc0b6b9ab..d7e6acdc51cba147537b884351b8b7343850b721 100644 (file)
@@ -9012,7 +9012,9 @@ resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
          if (as->corank != 0)
            sym->attr.codimension = 1;
        }
-      else if (sym->ts.type == BT_CLASS && (!CLASS_DATA (sym)->as || sym->assoc->rankguessed))
+      else if (sym->ts.type == BT_CLASS
+              && CLASS_DATA (sym)
+              && (!CLASS_DATA (sym)->as || sym->assoc->rankguessed))
        {
          if (!CLASS_DATA (sym)->as)
            CLASS_DATA (sym)->as = gfc_get_array_spec ();
diff --git a/gcc/testsuite/gfortran.dg/pr96086.f90 b/gcc/testsuite/gfortran.dg/pr96086.f90
new file mode 100644 (file)
index 0000000..b80967a
--- /dev/null
@@ -0,0 +1,8 @@
+! { dg-do compile }
+! PR fortran/96086 - ICE in gfc_match_select_rank, at fortran/match.c:6645
+
+subroutine s
+  class(*) :: x(..)     ! { dg-error "Assumed-rank array" }
+  select rank (y => x)  ! { dg-error "CLASS variable" }
+  end select
+end