From db7562ef764564560fcc59c192df5c00269382ac Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Mon, 3 Jan 2022 20:31:10 +0100
Subject: [PATCH] Fortran: reject invalid non-constant pointer initialization
targets
gcc/fortran/ChangeLog:
PR fortran/101762
* decl.c (match_pointer_init): Check that subscripts and substring
indices in specifications of pointer initialization targets are
constant expressions.
gcc/testsuite/ChangeLog:
PR fortran/101762
* gfortran.dg/pr101762.f90: New test.
---
gcc/fortran/decl.c | 34 ++++++++++++++++++++++++++
gcc/testsuite/gfortran.dg/pr101762.f90 | 23 +++++++++++++++++
2 files changed, 57 insertions(+)
create mode 100644 gcc/testsuite/gfortran.dg/pr101762.f90
@@ -2525,6 +2525,40 @@ match_pointer_init (gfc_expr **init, int procptr)
"initialization at %C"))
return MATCH_ERROR;
+ gfc_ref *ref = (*init)->ref;
+ for (; ref; ref = ref->next)
+ {
+ switch (ref->type)
+ {
+ case REF_ARRAY:
+ for (int n = 0; n < ref->u.ar.dimen; n++)
+ if (!gfc_is_constant_expr (ref->u.ar.start[n])
+ || !gfc_is_constant_expr (ref->u.ar.end[n])
+ || !gfc_is_constant_expr (ref->u.ar.stride[n]))
+ {
+ gfc_error ("Every subscript of target specification "
+ "at %L must be a constant expression",
+ &ref->u.ar.where);
+ return MATCH_ERROR;
+ }
+ break;
+
+ case REF_SUBSTRING:
+ if (!gfc_is_constant_expr (ref->u.ss.start)
+ || !gfc_is_constant_expr (ref->u.ss.end))
+ {
+ gfc_error ("Substring starting and ending points of target "
+ "specification at %L must be constant expressions",
+ &ref->u.ss.start->where);
+ return MATCH_ERROR;
+ }
+ break;
+
+ default:
+ break;
+ }
+ }
+
return MATCH_YES;
}
new file mode 100644
@@ -0,0 +1,23 @@
+! { dg-do compile }
+! PR fortran/101762 - ICE on non-constant pointer initialization targets
+! Contributed by G.Steinmetz
+
+program p
+ integer, target :: a(3) = [7, 8, 9]
+ integer, pointer :: x => a(3)
+ integer, pointer :: y => a(n()) ! { dg-error "constant expression" }
+ integer, pointer :: z => a(:n()) ! { dg-error "constant expression" }
+ character(7), target :: c = "abcdefg"
+ character(3), pointer :: c0 => c(2:4)
+ character(3), pointer :: c1 => c(m():) ! { dg-error "constant expression" }
+ character(3), pointer :: c2 => c(:m()) ! { dg-error "constant expression" }
+ print *, x
+contains
+ pure integer function k ()
+ k = 2
+ end function k
+ subroutine s ()
+ integer, pointer :: yy => a(k()) ! { dg-error "constant expression" }
+ print *, yy
+ end subroutine s
+end
--
2.31.1