public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r10-10419] Fortran: reject invalid non-constant pointer initialization targets
@ 2022-01-25 19:37 Harald Anlauf
  0 siblings, 0 replies; only message in thread
From: Harald Anlauf @ 2022-01-25 19:37 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:a5f7e0838e1573f4cc33a6f2c70c60187d7a63af

commit r10-10419-ga5f7e0838e1573f4cc33a6f2c70c60187d7a63af
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Sun Jan 9 22:08:14 2022 +0100

    Fortran: reject invalid non-constant pointer initialization targets
    
    gcc/fortran/ChangeLog:
    
            PR fortran/101762
            * expr.c (gfc_check_pointer_assign): For pointer initialization
            targets, check that subscripts and substring indices in
            specifications are constant expressions.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/101762
            * gfortran.dg/pr101762.f90: New test.
    
    (cherry picked from commit 2e63128306ff93d8f53119137dd6c28b2defac94)

Diff:
---
 gcc/fortran/expr.c                     | 34 ++++++++++++++++++++++++++++++++++
 gcc/testsuite/gfortran.dg/pr101762.f90 | 23 +++++++++++++++++++++++
 2 files changed, 57 insertions(+)

diff --git a/gcc/fortran/expr.c b/gcc/fortran/expr.c
index 278b07135e3..d19c6c9980b 100644
--- a/gcc/fortran/expr.c
+++ b/gcc/fortran/expr.c
@@ -4333,6 +4333,7 @@ gfc_check_pointer_assign (gfc_expr *lvalue, gfc_expr *rvalue,
     {
       gfc_symbol *sym;
       bool target;
+      gfc_ref *ref;
 
       if (gfc_is_size_zero_array (rvalue))
 	{
@@ -4362,6 +4363,39 @@ gfc_check_pointer_assign (gfc_expr *lvalue, gfc_expr *rvalue,
 		     &rvalue->where);
 	  return false;
 	}
+
+      for (ref = rvalue->ref; 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 false;
+		  }
+	      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 false;
+		}
+	      break;
+
+	    default:
+	      break;
+	    }
+	}
     }
   else
     {
diff --git a/gcc/testsuite/gfortran.dg/pr101762.f90 b/gcc/testsuite/gfortran.dg/pr101762.f90
new file mode 100644
index 00000000000..9ffd7540d81
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr101762.f90
@@ -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, y
+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


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-01-25 19:37 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-25 19:37 [gcc r10-10419] Fortran: reject invalid non-constant pointer initialization targets Harald Anlauf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).