Fortran: reject bad DIM argument of SIZE intrinsic in simplification [PR104350]
Checks
Commit Message
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
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.
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
@@ -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;
}
new file mode 100644
@@ -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