From 9f7ba2088e4ebfa747f51a107d25f05c29df51b7 Mon Sep 17 00:00:00 2001 From: Louis Krupp Date: Mon, 24 Aug 2015 16:29:59 +0000 Subject: [PATCH] re PR fortran/62536 (ICE (segfault) for invalid END BLOCK statement) PR fortran/62536 PR fortran/66175 * decl.c (gfc_match_end): Clean up nested BLOCKs. * parse.c (parse_block_construct): Deal gracefully with cleaned-up BLOCKs. * gfortran.dg/block_end_error_1.f90: New test. * gfortran.dg/blocks_nested_incomplete_1.f90: New test. From-SVN: r227135 --- gcc/fortran/ChangeLog | 8 ++++++++ gcc/fortran/decl.c | 4 +++- gcc/fortran/parse.c | 7 ++++++- gcc/testsuite/ChangeLog | 7 +++++++ gcc/testsuite/gfortran.dg/block_end_error_1.f90 | 10 ++++++++++ gcc/testsuite/gfortran.dg/blocks_nested_incomplete_1.f90 | 9 +++++++++ 6 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/block_end_error_1.f90 create mode 100644 gcc/testsuite/gfortran.dg/blocks_nested_incomplete_1.f90 diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog index 9f2e05c..9ca5f8d 100644 --- a/gcc/fortran/ChangeLog +++ b/gcc/fortran/ChangeLog @@ -1,3 +1,11 @@ +2015-08-24 Louis Krupp + + PR fortran/62536 + PR fortran/66175 + * decl.c (gfc_match_end): Clean up nested BLOCKs. + * parse.c (parse_block_construct): Deal gracefully with cleaned-up + BLOCKs. + 2015-08-23 Francois-Xavier Coudert PR libfortran/54572 diff --git a/gcc/fortran/decl.c b/gcc/fortran/decl.c index ebc88ea..6829d17 100644 --- a/gcc/fortran/decl.c +++ b/gcc/fortran/decl.c @@ -6483,7 +6483,7 @@ cleanup: /* If we are missing an END BLOCK, we created a half-ready namespace. Remove it from the parent namespace's sibling list. */ - if (state == COMP_BLOCK) + while (state == COMP_BLOCK) { parent_ns = gfc_current_ns->parent; @@ -6506,6 +6506,8 @@ cleanup: gfc_free_namespace (gfc_current_ns); gfc_current_ns = parent_ns; + gfc_state_stack = gfc_state_stack->previous; + state = gfc_current_state (); } return MATCH_ERROR; diff --git a/gcc/fortran/parse.c b/gcc/fortran/parse.c index 04b4c80..62bcb61 100644 --- a/gcc/fortran/parse.c +++ b/gcc/fortran/parse.c @@ -3935,6 +3935,7 @@ static void parse_block_construct (void) { gfc_namespace* my_ns; + gfc_namespace* my_parent; gfc_state_data s; gfc_notify_std (GFC_STD_F2008, "BLOCK construct at %C"); @@ -3948,10 +3949,14 @@ parse_block_construct (void) push_state (&s, COMP_BLOCK, my_ns->proc_name); gfc_current_ns = my_ns; + my_parent = my_ns->parent; parse_progunit (ST_NONE); - gfc_current_ns = gfc_current_ns->parent; + /* Don't depend on the value of gfc_current_ns; it might have been + reset if the block had errors and was cleaned up. */ + gfc_current_ns = my_parent; + pop_state (); } diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 0cb8082..50aa63b 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,10 @@ +2015-08-24 Louis Krupp + + PR fortran/62536 + PR fortran/66175 + * gfortran.dg/block_end_error_1.f90: New test. + * gfortran.dg/blocks_nested_incomplete_1.f90: New test. + 2015-08-24 H.J. Lu PR target/66821 diff --git a/gcc/testsuite/gfortran.dg/block_end_error_1.f90 b/gcc/testsuite/gfortran.dg/block_end_error_1.f90 new file mode 100644 index 0000000..e7dcdfa --- /dev/null +++ b/gcc/testsuite/gfortran.dg/block_end_error_1.f90 @@ -0,0 +1,10 @@ +! { dg-do compile } +! +! PR fortran/62536 +! Bad "end block" causes ICE. +subroutine s + block + end block named ! { dg-error "Syntax error in END BLOCK statement" } + return +endsubroutine +! { dg-prune-output "Unexpected end of file" } diff --git a/gcc/testsuite/gfortran.dg/blocks_nested_incomplete_1.f90 b/gcc/testsuite/gfortran.dg/blocks_nested_incomplete_1.f90 new file mode 100644 index 0000000..ccf6c3f --- /dev/null +++ b/gcc/testsuite/gfortran.dg/blocks_nested_incomplete_1.f90 @@ -0,0 +1,9 @@ +! { dg-do compile } +! +! PR fortran/66175 +! Nested incomplete blocks cause ICE. +program main + block + block +end program ! { dg-error "Expecting END BLOCK statement" } +! { dg-prune-output "Unexpected end of file" } -- 2.7.4