public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] PR fortran/101762 - ICE on non-constant pointer initialization targets
@ 2022-01-03 19:45 Harald Anlauf
  2022-01-09 19:28 ` Mikael Morin
  0 siblings, 1 reply; 4+ messages in thread
From: Harald Anlauf @ 2022-01-03 19:45 UTC (permalink / raw)
  To: fortran, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 447 bytes --]

Dear all,

the initial-data-target for a pointer initialization can be either
NULL() or a non-constant target.  In the latter case subscripts of
the target specification (or substring starting and ending points)
must be constant expressions.  The patch adds corresponding checks.

I have verified that current Intel and Cray compilers generate similar
errors for the testcase.

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

Thanks,
Harald


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Fortran-reject-invalid-non-constant-pointer-initiali.patch --]
[-- Type: text/x-patch, Size: 3106 bytes --]

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

diff --git a/gcc/fortran/decl.c b/gcc/fortran/decl.c
index 4e510cc72ef..4a52a908ac8 100644
--- a/gcc/fortran/decl.c
+++ b/gcc/fortran/decl.c
@@ -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;
 }

diff --git a/gcc/testsuite/gfortran.dg/pr101762.f90 b/gcc/testsuite/gfortran.dg/pr101762.f90
new file mode 100644
index 00000000000..14bcee9ec00
--- /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
+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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] PR fortran/101762 - ICE on non-constant pointer initialization targets
  2022-01-03 19:45 [PATCH] PR fortran/101762 - ICE on non-constant pointer initialization targets Harald Anlauf
@ 2022-01-09 19:28 ` Mikael Morin
  2022-01-09 21:13   ` Harald Anlauf
  0 siblings, 1 reply; 4+ messages in thread
From: Mikael Morin @ 2022-01-09 19:28 UTC (permalink / raw)
  To: Harald Anlauf, fortran, gcc-patches

Le 03/01/2022 à 20:45, Harald Anlauf via Fortran a écrit :
> Dear all,
> 
> the initial-data-target for a pointer initialization can be either
> NULL() or a non-constant target.  In the latter case subscripts of
> the target specification (or substring starting and ending points)
> must be constant expressions.  The patch adds corresponding checks.
> 
> I have verified that current Intel and Cray compilers generate similar
> errors for the testcase.
> 
> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
> 
Hello,

There is gfc_check_pointer_assign which does already various checks 
relating to pointer assignment, and those with is_init_expr == true 
could be tightened a bit.

OK with your additional checks moved there.
Thanks.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] PR fortran/101762 - ICE on non-constant pointer initialization targets
  2022-01-09 19:28 ` Mikael Morin
@ 2022-01-09 21:13   ` Harald Anlauf
  2022-01-09 21:13     ` Harald Anlauf
  0 siblings, 1 reply; 4+ messages in thread
From: Harald Anlauf @ 2022-01-09 21:13 UTC (permalink / raw)
  To: Mikael Morin, fortran, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 1006 bytes --]

Hi Mikael,

Am 09.01.22 um 20:28 schrieb Mikael Morin:
> Le 03/01/2022 à 20:45, Harald Anlauf via Fortran a écrit :
>> Dear all,
>>
>> the initial-data-target for a pointer initialization can be either
>> NULL() or a non-constant target.  In the latter case subscripts of
>> the target specification (or substring starting and ending points)
>> must be constant expressions.  The patch adds corresponding checks.
>>
>> I have verified that current Intel and Cray compilers generate similar
>> errors for the testcase.
>>
>> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
>>
> Hello,
>
> There is gfc_check_pointer_assign which does already various checks
> relating to pointer assignment, and those with is_init_expr == true
> could be tightened a bit.

agreed, this is a more appropriate location for this kind of checks.

> OK with your additional checks moved there.
> Thanks.

Done so and pushed as r12-6387, see attached updated patch.

Thanks for the review!

[-- Attachment #2: pr101762-v2.diff --]
[-- Type: text/x-patch, Size: 3235 bytes --]

From 2e63128306ff93d8f53119137dd6c28b2defac94 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Sun, 9 Jan 2022 22:08:14 +0100
Subject: [PATCH] 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.
---
 gcc/fortran/expr.c                     | 34 ++++++++++++++++++++++++++
 gcc/testsuite/gfortran.dg/pr101762.f90 | 23 +++++++++++++++++
 2 files changed, 57 insertions(+)
 create mode 100644 gcc/testsuite/gfortran.dg/pr101762.f90

diff --git a/gcc/fortran/expr.c b/gcc/fortran/expr.c
index 96a2cd70900..a87686d8217 100644
--- a/gcc/fortran/expr.c
+++ b/gcc/fortran/expr.c
@@ -4343,6 +4343,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))
 	{
@@ -4372,6 +4373,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
-- 
2.31.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] PR fortran/101762 - ICE on non-constant pointer initialization targets
  2022-01-09 21:13   ` Harald Anlauf
@ 2022-01-09 21:13     ` Harald Anlauf
  0 siblings, 0 replies; 4+ messages in thread
From: Harald Anlauf @ 2022-01-09 21:13 UTC (permalink / raw)
  To: fortran; +Cc: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 979 bytes --]

Hi Mikael,

Am 09.01.22 um 20:28 schrieb Mikael Morin:
> Le 03/01/2022 à 20:45, Harald Anlauf via Fortran a écrit :
>> Dear all,
>>
>> the initial-data-target for a pointer initialization can be either
>> NULL() or a non-constant target.  In the latter case subscripts of
>> the target specification (or substring starting and ending points)
>> must be constant expressions.  The patch adds corresponding checks.
>>
>> I have verified that current Intel and Cray compilers generate similar
>> errors for the testcase.
>>
>> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
>>
> Hello,
> 
> There is gfc_check_pointer_assign which does already various checks 
> relating to pointer assignment, and those with is_init_expr == true 
> could be tightened a bit.

agreed, this is a more appropriate location for this kind of checks.

> OK with your additional checks moved there.
> Thanks.

Done so and pushed as r12-6387, see attached updated patch.

Thanks for the review!

[-- Attachment #2: pr101762-v2.diff --]
[-- Type: text/x-patch, Size: 3235 bytes --]

From 2e63128306ff93d8f53119137dd6c28b2defac94 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Sun, 9 Jan 2022 22:08:14 +0100
Subject: [PATCH] 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.
---
 gcc/fortran/expr.c                     | 34 ++++++++++++++++++++++++++
 gcc/testsuite/gfortran.dg/pr101762.f90 | 23 +++++++++++++++++
 2 files changed, 57 insertions(+)
 create mode 100644 gcc/testsuite/gfortran.dg/pr101762.f90

diff --git a/gcc/fortran/expr.c b/gcc/fortran/expr.c
index 96a2cd70900..a87686d8217 100644
--- a/gcc/fortran/expr.c
+++ b/gcc/fortran/expr.c
@@ -4343,6 +4343,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))
 	{
@@ -4372,6 +4373,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
-- 
2.31.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-01-09 21:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-03 19:45 [PATCH] PR fortran/101762 - ICE on non-constant pointer initialization targets Harald Anlauf
2022-01-09 19:28 ` Mikael Morin
2022-01-09 21:13   ` Harald Anlauf
2022-01-09 21:13     ` 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).