Fortran: reject bad DIM argument of SIZE intrinsic in simplification [PR104350]

Message ID trinity-d99cc8a9-1e19-4b84-89aa-f8a2c36b6ae1-1684955762561@3c-app-gmx-bap32
State Accepted
Headers
Series Fortran: reject bad DIM argument of SIZE intrinsic in simplification [PR104350] |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Harald Anlauf May 24, 2023, 7:16 p.m. UTC
  Dear all,

the attached almost obvious patch fixes an ICE on invalid that may
occur when we attempt to simplify an initialization expression with
SIZE for an out-of-range DIM argument.  Returning gfc_bad_expr
allows for a more graceful error recovery.

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

Thanks,
Harald
  

Comments

Mikael Morin May 24, 2023, 7:47 p.m. UTC | #1
Le 24/05/2023 à 21:16, Harald Anlauf via Fortran a écrit :
> Dear all,
> 
> the attached almost obvious patch fixes an ICE on invalid that may
> occur when we attempt to simplify an initialization expression with
> SIZE for an out-of-range DIM argument.  Returning gfc_bad_expr
> allows for a more graceful error recovery.
> 
> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
> 
OK, thanks.
  

Patch

From 738bdcce46bd760fcafd1eb56700c8824621266f Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Wed, 24 May 2023 21:04:43 +0200
Subject: [PATCH] Fortran: reject bad DIM argument of SIZE intrinsic in
 simplification [PR104350]

gcc/fortran/ChangeLog:

	PR fortran/104350
	* simplify.cc (simplify_size): Reject DIM argument of intrinsic SIZE
	with error when out of valid range.

gcc/testsuite/ChangeLog:

	PR fortran/104350
	* gfortran.dg/size_dim_2.f90: New test.
---
 gcc/fortran/simplify.cc                  | 12 +++++++++++-
 gcc/testsuite/gfortran.dg/size_dim_2.f90 | 19 +++++++++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gfortran.dg/size_dim_2.f90

diff --git a/gcc/fortran/simplify.cc b/gcc/fortran/simplify.cc
index 3f77203e62e..81680117f70 100644
--- a/gcc/fortran/simplify.cc
+++ b/gcc/fortran/simplify.cc
@@ -7594,7 +7594,17 @@  simplify_size (gfc_expr *array, gfc_expr *dim, int k)
       if (dim->expr_type != EXPR_CONSTANT)
 	return NULL;

-      d = mpz_get_ui (dim->value.integer) - 1;
+      if (array->rank == -1)
+	return NULL;
+
+      d = mpz_get_si (dim->value.integer) - 1;
+      if (d < 0 || d > array->rank - 1)
+	{
+	  gfc_error ("DIM argument (%d) to intrinsic SIZE at %L out of range "
+		     "(1:%d)", d+1, &array->where, array->rank);
+	  return &gfc_bad_expr;
+	}
+
       if (!gfc_array_dimen_size (array, d, &size))
 	return NULL;
     }
diff --git a/gcc/testsuite/gfortran.dg/size_dim_2.f90 b/gcc/testsuite/gfortran.dg/size_dim_2.f90
new file mode 100644
index 00000000000..27a71d90a47
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/size_dim_2.f90
@@ -0,0 +1,19 @@ 
+! { dg-do compile }
+! PR fortran/104350 - ICE with SIZE and bad DIM in initialization expression
+! Contributed by G. Steinmetz
+
+program p
+  implicit none
+  integer :: k
+  integer, parameter :: x(2,3) = 42
+  integer, parameter :: s(*) = [(size(x,dim=k),k=1,rank(x))]
+  integer, parameter :: t(*) = [(size(x,dim=k),k=1,3)]   ! { dg-error "out of range" }
+  integer, parameter :: u(*) = [(size(x,dim=k),k=0,3)]   ! { dg-error "out of range" }
+  integer, parameter :: v = product(shape(x))
+  integer, parameter :: w = product([(size(x,k),k=0,3)]) ! { dg-error "out of range" }
+  print *,        ([(size(x,dim=k),k=1,rank(x))])
+  print *,         [(size(x,dim=k),k=1,rank(x))]
+  print *,         [(size(x,dim=k),k=0,rank(x))]
+  print *, product([(size(x,dim=k),k=1,rank(x))])
+  print *, product([(size(x,dim=k),k=0,rank(x))])
+end
--
2.35.3