PR fortran/103588 - ICE: Simplification error in gfc_ref_dimen_size, at fortran/array.c:2407

Message ID trinity-1294a803-77f7-46fc-b2c7-5e70efc32b5c-1638830387025@3c-app-gmx-bap31
State New
Headers
Series PR fortran/103588 - ICE: Simplification error in gfc_ref_dimen_size, at fortran/array.c:2407 |

Commit Message

Harald Anlauf Dec. 6, 2021, 10:39 p.m. UTC
  Dear all,

using a bad expression as stride in a subsequent array section did
lead to an internal error which was directly invoked after the
failure.  We better return a failure code to let error recovery
do its expected job.

Regtested on x86_64-pc-linux-gnu.  OK for mainline?

Thanks,
Harald
  

Comments

Mikael Morin Dec. 7, 2021, 12:29 p.m. UTC | #1
Hello,

On 06/12/2021 23:39, Harald Anlauf via Fortran wrote:
> diff --git a/gcc/fortran/array.c b/gcc/fortran/array.c
> index 5762c8d92d4..5f9ed17f919 100644
> --- a/gcc/fortran/array.c
> +++ b/gcc/fortran/array.c
> @@ -2403,11 +2403,9 @@ gfc_ref_dimen_size (gfc_array_ref *ar, int dimen, mpz_t *result, mpz_t *end)
>  	{
>  	  stride_expr = gfc_copy_expr(ar->stride[dimen]);
> 
> -	  if(!gfc_simplify_expr(stride_expr, 1))
> -	    gfc_internal_error("Simplification error");
> -
> -	  if (stride_expr->expr_type != EXPR_CONSTANT
> -	      || mpz_cmp_ui (stride_expr->value.integer, 0) == 0)
> +	  if (!gfc_simplify_expr (stride_expr, 1)
> +	     || stride_expr->expr_type != EXPR_CONSTANT
> +	     || mpz_cmp_ui (stride_expr->value.integer, 0) == 0)
>  	    {
>  	      mpz_clear (stride);
>  	      return false;

it’s a preexisting problem, but stride_expr should be freed before 
returning false above.

OK with that change.

Thanks.
  

Patch

From 1487d327b13b45acca79c0c691a748ca1a50bc04 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Mon, 6 Dec 2021 23:34:17 +0100
Subject: [PATCH] Fortran: catch failed simplification of	bad stride
 expression

gcc/fortran/ChangeLog:

	PR fortran/103588
	* array.c (gfc_ref_dimen_size): Do not generate internal error on
	failed simplification of stride expression; just return failure.

gcc/testsuite/ChangeLog:

	PR fortran/103588
	* gfortran.dg/pr103588.f90: New test.
---
 gcc/fortran/array.c                    | 8 +++-----
 gcc/testsuite/gfortran.dg/pr103588.f90 | 8 ++++++++
 2 files changed, 11 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/pr103588.f90

diff --git a/gcc/fortran/array.c b/gcc/fortran/array.c
index 5762c8d92d4..5f9ed17f919 100644
--- a/gcc/fortran/array.c
+++ b/gcc/fortran/array.c
@@ -2403,11 +2403,9 @@  gfc_ref_dimen_size (gfc_array_ref *ar, int dimen, mpz_t *result, mpz_t *end)
 	{
 	  stride_expr = gfc_copy_expr(ar->stride[dimen]);

-	  if(!gfc_simplify_expr(stride_expr, 1))
-	    gfc_internal_error("Simplification error");
-
-	  if (stride_expr->expr_type != EXPR_CONSTANT
-	      || mpz_cmp_ui (stride_expr->value.integer, 0) == 0)
+	  if (!gfc_simplify_expr (stride_expr, 1)
+	     || stride_expr->expr_type != EXPR_CONSTANT
+	     || mpz_cmp_ui (stride_expr->value.integer, 0) == 0)
 	    {
 	      mpz_clear (stride);
 	      return false;
diff --git a/gcc/testsuite/gfortran.dg/pr103588.f90 b/gcc/testsuite/gfortran.dg/pr103588.f90
new file mode 100644
index 00000000000..198e1766cd2
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr103588.f90
@@ -0,0 +1,8 @@ 
+! { dg-do compile }
+! PR fortran/103588 - ICE: Simplification error in gfc_ref_dimen_size
+! Contributed by G.Steinmetz
+
+program p
+  integer, parameter :: a(:) = [1,2] ! { dg-error "cannot be automatic or of deferred shape" }
+  integer :: b(2) = a(::a(1))        ! { dg-error "Invalid" }
+end
--
2.26.2