From 8a0b69f0b089c05d233b8e1a941825b1ceac93bd Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Fri, 10 Jul 2020 21:00:13 +0200 Subject: [PATCH] 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 an illegal declaration. --- gcc/fortran/match.c | 5 +++-- gcc/fortran/resolve.c | 4 +++- gcc/testsuite/gfortran.dg/pr96086.f90 | 8 ++++++++ 3 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/pr96086.f90 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 6bc1c46a97d..d7e6acdc51c 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 -- 2.30.2