PR fortran/96086 - ICE in gfc_match_select_rank, at fortran/match.c:6645

Message ID trinity-03edb5dd-00d0-483c-b3f9-0c716f86c343-1594066300392@3c-app-gmx-bap17
State New
Headers show
Series
  • PR fortran/96086 - ICE in gfc_match_select_rank, at fortran/match.c:6645
Related show

Commit Message

Harald Anlauf July 6, 2020, 8:11 p.m.
More NULL pointer dereferences on invalid code, detected by Gerhard.

Regtested on x86_64-pc-linux-gnu.

OK for master?

Thanks,
Harald


PR fortran/96086 - ICE in gfc_match_select_rank, at fortran/match.c:6645

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 a illegal declaration.

Comments

Jason Merrill via Gcc-patches July 10, 2020, 5:07 p.m. | #1
Am 06.07.20 um 22:11 schrieb Harald Anlauf:
> More NULL pointer dereferences on invalid code, detected by Gerhard.

> 

> Regtested on x86_64-pc-linux-gnu.

> 

> OK for master?


OK.

This is actually a regression (does not occur with gcc-8), so if you
feel like it, feel free to backport this.  However, if you do that,
I would appreciate if you could wait until gcc10 is again in
normal regression fixing mode.

Thanks for the patch!

Best regards

	Thomas

Patch

diff --git a/gcc/fortran/match.c b/gcc/fortran/match.c
index 7d3711c55f9..cb09c5f8ec5 100644
--- a/gcc/fortran/match.c
+++ b/gcc/fortran/match.c
@@ -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);

diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c
index 223dcccce91..ef251dc142d 100644
--- a/gcc/fortran/resolve.c
+++ b/gcc/fortran/resolve.c
@@ -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
index 00000000000..b80967a7a07
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr96086.f90
@@ -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