public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] PR fortran/105184 - ICE in gfc_array_init_size, at fortran/trans-array.cc:5841
@ 2022-04-06 20:30 Harald Anlauf
  2022-04-08 20:44 ` [PATCH, v2] " Harald Anlauf
  0 siblings, 1 reply; 5+ messages in thread
From: Harald Anlauf @ 2022-04-06 20:30 UTC (permalink / raw)
  To: fortran, gcc-patches

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

Dear all,

the logic for checking the allocate-coshape-spec in an ALLOCATE
statement was sort of sideways, and allowed to pass invalid
specifications to the code generation.

The fix seems obvious (to me).

Regtested on x86_64-pc-linux-gnu.  OK for mainline?
(12 or wait for 13?).

Thanks,
Harald


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Fortran-fix-logic-for-checking-coshape-specification.patch --]
[-- Type: text/x-patch, Size: 3085 bytes --]

From 2adcdbd40e3a64d1f2d42eb0e0fdcc7e593da137 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Wed, 6 Apr 2022 22:24:21 +0200
Subject: [PATCH] Fortran: fix logic for checking coshape specification in
 ALLOCATE statement

gcc/fortran/ChangeLog:

	PR fortran/105184
	* resolve.cc (resolve_allocate_expr): Fix logic for checking
	allocate-coshape-spec in ALLOCATE statement.

gcc/testsuite/ChangeLog:

	PR fortran/105184
	* gfortran.dg/coarray_44.f90: Adjust expected output.
	* gfortran.dg/coarray_50.f90: New test.
---
 gcc/fortran/resolve.cc                   | 10 +++++-----
 gcc/testsuite/gfortran.dg/coarray_44.f90 |  2 ++
 gcc/testsuite/gfortran.dg/coarray_50.f90 | 17 +++++++++++++++++
 3 files changed, 24 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/coarray_50.f90

diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index 21c8797c938..45a04dab703 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -8108,12 +8108,12 @@ resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
 	    goto failure;

 	  case  DIMEN_RANGE:
-	    if (ar->start[i] == 0 || ar->end[i] == 0)
+	    // F2018:R937:
+	    // allocate-coshape-spec is [ lower-bound-expr : ] upper-bound-expr
+	    if (ar->start[i] == 0 || ar->end[i] == 0 || ar->stride[i] != NULL)
 	      {
-		/* If ar->stride[i] is NULL, we issued a previous error.  */
-		if (ar->stride[i] == NULL)
-		  gfc_error ("Bad array specification in ALLOCATE statement "
-			     "at %L", &e->where);
+		gfc_error ("Bad array specification in ALLOCATE statement "
+			   "at %L", &e->where);
 		goto failure;
 	      }
 	    else if (gfc_dep_compare_expr (ar->start[i], ar->end[i]) == 1)
diff --git a/gcc/testsuite/gfortran.dg/coarray_44.f90 b/gcc/testsuite/gfortran.dg/coarray_44.f90
index 15fb8c76ce4..f83e3e9b19d 100644
--- a/gcc/testsuite/gfortran.dg/coarray_44.f90
+++ b/gcc/testsuite/gfortran.dg/coarray_44.f90
@@ -10,3 +10,5 @@ program pr70071
   allocate (z(2)[1::2,*])  ! { dg-error "Bad array dimension" }
   allocate (z(1::2)[2,*])  ! { dg-error "Bad array specification in ALLOCATE" }
 end program pr70071
+
+! { dg-prune-output "Bad array specification in ALLOCATE statement" }
diff --git a/gcc/testsuite/gfortran.dg/coarray_50.f90 b/gcc/testsuite/gfortran.dg/coarray_50.f90
new file mode 100644
index 00000000000..9e8bd5d53de
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/coarray_50.f90
@@ -0,0 +1,17 @@
+! { dg-do compile }
+! { dg-options "-fcoarray=single" }
+!
+! PR fortran/105184
+! Based on testcases by Gerhard Steinmetz
+
+program p
+  real, allocatable :: x[:,:]
+  integer :: n = 2
+  allocate (x[  n, *])
+  allocate (x[1:n, *])
+  allocate (x[n:n, *])
+  allocate (x[ :n,   *]) ! { dg-error "Bad array specification" }
+  allocate (x[::n,   *]) ! { dg-error "Bad array specification" }
+  allocate (x[ :1:1, *]) ! { dg-error "Bad array specification" }
+  allocate (x[1:n:n, *]) ! { dg-error "Bad array specification" }
+end
--
2.34.1


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

* [PATCH, v2] PR fortran/105184 - ICE in gfc_array_init_size, at fortran/trans-array.cc:5841
  2022-04-06 20:30 [PATCH] PR fortran/105184 - ICE in gfc_array_init_size, at fortran/trans-array.cc:5841 Harald Anlauf
@ 2022-04-08 20:44 ` Harald Anlauf
  2022-04-08 20:44   ` Harald Anlauf
                     ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Harald Anlauf @ 2022-04-08 20:44 UTC (permalink / raw)
  To: fortran, gcc-patches

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

Dear all,

Am 06.04.22 um 22:30 schrieb Harald Anlauf via Fortran:
> Dear all,
>
> the logic for checking the allocate-coshape-spec in an ALLOCATE
> statement was sort of sideways, and allowed to pass invalid
> specifications to the code generation.
>
> The fix seems obvious (to me).

after submitting the previous patch, I found another invalid case with
a missing lower bound which was still silently accepted.  The attached
revised patch adds this check, improves the original error message to
actually point to the coarray specification, and renames the testcase
to align better with existing coarray testcases.

> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
> (12 or wait for 13?).

Regtested again with no new failures.  OK for mainline?

Thanks,
Harald

[-- Attachment #2: 0001-Fortran-fix-checking-of-coshape-specification-in-ALL.patch --]
[-- Type: text/x-patch, Size: 5043 bytes --]

From 483cbf9942dcfcf74a912312dbbcda2f108200ea Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Wed, 6 Apr 2022 22:24:21 +0200
Subject: [PATCH] Fortran: fix checking of coshape specification in ALLOCATE
 statement

gcc/fortran/ChangeLog:

	PR fortran/105184
	* array.cc (match_subscript): Reject assumed size coarray
	specification with missing lower bound.
	* resolve.cc (resolve_allocate_expr): Fix logic for checking
	allocate-coshape-spec in ALLOCATE statement.

gcc/testsuite/ChangeLog:

	PR fortran/105184
	* gfortran.dg/coarray_44.f90: Adjust expected output.
	* gfortran.dg/coarray_allocate_11.f90: Likewise.
	* gfortran.dg/coarray_allocate_12.f90: New test.
---
 gcc/fortran/array.cc                          |  7 +++++++
 gcc/fortran/resolve.cc                        | 11 ++++++-----
 gcc/testsuite/gfortran.dg/coarray_44.f90      |  2 ++
 .../gfortran.dg/coarray_allocate_11.f90       |  6 +++---
 .../gfortran.dg/coarray_allocate_12.f90       | 19 +++++++++++++++++++
 5 files changed, 37 insertions(+), 8 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/coarray_allocate_12.f90

diff --git a/gcc/fortran/array.cc b/gcc/fortran/array.cc
index eb9ed8580a0..90ea812d699 100644
--- a/gcc/fortran/array.cc
+++ b/gcc/fortran/array.cc
@@ -134,6 +134,13 @@ end_element:
   if (m == MATCH_ERROR)
     return MATCH_ERROR;
 
+  if (star && ar->start[i] == NULL)
+    {
+      gfc_error ("Missing lower bound in assumed size "
+		 "coarray specification at %C");
+      return MATCH_ERROR;
+    }
+
   /* See if we have an optional stride.  */
   if (gfc_match_char (':') == MATCH_YES)
     {
diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index 21c8797c938..05f8f1bf6c2 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -8108,12 +8108,13 @@ resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
 	    goto failure;
 
 	  case  DIMEN_RANGE:
-	    if (ar->start[i] == 0 || ar->end[i] == 0)
+	    /* F2018:R937:
+	     * allocate-coshape-spec is [ lower-bound-expr : ] upper-bound-expr
+	     */
+	    if (ar->start[i] == 0 || ar->end[i] == 0 || ar->stride[i] != NULL)
 	      {
-		/* If ar->stride[i] is NULL, we issued a previous error.  */
-		if (ar->stride[i] == NULL)
-		  gfc_error ("Bad array specification in ALLOCATE statement "
-			     "at %L", &e->where);
+		gfc_error ("Bad coarray specification in ALLOCATE statement "
+			   "at %L", &e->where);
 		goto failure;
 	      }
 	    else if (gfc_dep_compare_expr (ar->start[i], ar->end[i]) == 1)
diff --git a/gcc/testsuite/gfortran.dg/coarray_44.f90 b/gcc/testsuite/gfortran.dg/coarray_44.f90
index 15fb8c76ce4..545b5462251 100644
--- a/gcc/testsuite/gfortran.dg/coarray_44.f90
+++ b/gcc/testsuite/gfortran.dg/coarray_44.f90
@@ -10,3 +10,5 @@ program pr70071
   allocate (z(2)[1::2,*])  ! { dg-error "Bad array dimension" }
   allocate (z(1::2)[2,*])  ! { dg-error "Bad array specification in ALLOCATE" }
 end program pr70071
+
+! { dg-prune-output "Bad coarray specification in ALLOCATE statement" }
diff --git a/gcc/testsuite/gfortran.dg/coarray_allocate_11.f90 b/gcc/testsuite/gfortran.dg/coarray_allocate_11.f90
index 0e806f0955b..0e4f64e1e3d 100644
--- a/gcc/testsuite/gfortran.dg/coarray_allocate_11.f90
+++ b/gcc/testsuite/gfortran.dg/coarray_allocate_11.f90
@@ -3,10 +3,10 @@
 program p
    integer, allocatable :: z[:,:]
    integer :: i
-   allocate (z[1:,*]) ! { dg-error "Bad array specification in ALLOCATE statement" }
-   allocate (z[:2,*]) ! { dg-error "Bad array specification in ALLOCATE statement" }
+   allocate (z[1:,*]) ! { dg-error "Bad coarray specification in ALLOCATE statement" }
+   allocate (z[:2,*]) ! { dg-error "Bad coarray specification in ALLOCATE statement" }
    allocate (z[2:1,*]) ! { dg-error "Upper cobound is less than lower cobound" }
-   allocate (z[:0,*]) ! { dg-error "Bad array specification in ALLOCATE statement" }
+   allocate (z[:0,*]) ! { dg-error "Bad coarray specification in ALLOCATE statement" }
    allocate (z[0,*]) ! { dg-error "Upper cobound is less than lower cobound" }
    allocate (z[1,*]) ! This is OK
    allocate (z[1:1,*]) ! This is OK
diff --git a/gcc/testsuite/gfortran.dg/coarray_allocate_12.f90 b/gcc/testsuite/gfortran.dg/coarray_allocate_12.f90
new file mode 100644
index 00000000000..2169aa19998
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/coarray_allocate_12.f90
@@ -0,0 +1,19 @@
+! { dg-do compile }
+! { dg-options "-fcoarray=single" }
+!
+! PR fortran/105184
+! Based on testcases by Gerhard Steinmetz
+
+program p
+  real, allocatable :: x[:,:]
+  integer :: n = 2
+  allocate (x[  n, *])
+  allocate (x[1:n, *])
+  allocate (x[n:n, *])
+  allocate (x[n, 5:*])
+  allocate (x[ :n,   *]) ! { dg-error "Bad coarray specification" }
+  allocate (x[::n,   *]) ! { dg-error "Bad coarray specification" }
+  allocate (x[ :1:1, *]) ! { dg-error "Bad coarray specification" }
+  allocate (x[1:n:n, *]) ! { dg-error "Bad coarray specification" }
+  allocate (x[1,   : *]) ! { dg-error "Missing lower bound" }
+end
-- 
2.34.1


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

* [PATCH, v2] PR fortran/105184 - ICE in gfc_array_init_size, at fortran/trans-array.cc:5841
  2022-04-08 20:44 ` [PATCH, v2] " Harald Anlauf
@ 2022-04-08 20:44   ` Harald Anlauf
  2022-04-10  7:45   ` Paul Richard Thomas
  2022-04-10  8:07   ` Thomas Koenig
  2 siblings, 0 replies; 5+ messages in thread
From: Harald Anlauf @ 2022-04-08 20:44 UTC (permalink / raw)
  To: fortran; +Cc: gcc-patches

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

Dear all,

Am 06.04.22 um 22:30 schrieb Harald Anlauf via Fortran:
> Dear all,
> 
> the logic for checking the allocate-coshape-spec in an ALLOCATE
> statement was sort of sideways, and allowed to pass invalid
> specifications to the code generation.
> 
> The fix seems obvious (to me).

after submitting the previous patch, I found another invalid case with
a missing lower bound which was still silently accepted.  The attached
revised patch adds this check, improves the original error message to
actually point to the coarray specification, and renames the testcase
to align better with existing coarray testcases.

> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
> (12 or wait for 13?).

Regtested again with no new failures.  OK for mainline?

Thanks,
Harald

[-- Attachment #2: 0001-Fortran-fix-checking-of-coshape-specification-in-ALL.patch --]
[-- Type: text/x-patch, Size: 5043 bytes --]

From 483cbf9942dcfcf74a912312dbbcda2f108200ea Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Wed, 6 Apr 2022 22:24:21 +0200
Subject: [PATCH] Fortran: fix checking of coshape specification in ALLOCATE
 statement

gcc/fortran/ChangeLog:

	PR fortran/105184
	* array.cc (match_subscript): Reject assumed size coarray
	specification with missing lower bound.
	* resolve.cc (resolve_allocate_expr): Fix logic for checking
	allocate-coshape-spec in ALLOCATE statement.

gcc/testsuite/ChangeLog:

	PR fortran/105184
	* gfortran.dg/coarray_44.f90: Adjust expected output.
	* gfortran.dg/coarray_allocate_11.f90: Likewise.
	* gfortran.dg/coarray_allocate_12.f90: New test.
---
 gcc/fortran/array.cc                          |  7 +++++++
 gcc/fortran/resolve.cc                        | 11 ++++++-----
 gcc/testsuite/gfortran.dg/coarray_44.f90      |  2 ++
 .../gfortran.dg/coarray_allocate_11.f90       |  6 +++---
 .../gfortran.dg/coarray_allocate_12.f90       | 19 +++++++++++++++++++
 5 files changed, 37 insertions(+), 8 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/coarray_allocate_12.f90

diff --git a/gcc/fortran/array.cc b/gcc/fortran/array.cc
index eb9ed8580a0..90ea812d699 100644
--- a/gcc/fortran/array.cc
+++ b/gcc/fortran/array.cc
@@ -134,6 +134,13 @@ end_element:
   if (m == MATCH_ERROR)
     return MATCH_ERROR;
 
+  if (star && ar->start[i] == NULL)
+    {
+      gfc_error ("Missing lower bound in assumed size "
+		 "coarray specification at %C");
+      return MATCH_ERROR;
+    }
+
   /* See if we have an optional stride.  */
   if (gfc_match_char (':') == MATCH_YES)
     {
diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index 21c8797c938..05f8f1bf6c2 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -8108,12 +8108,13 @@ resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
 	    goto failure;
 
 	  case  DIMEN_RANGE:
-	    if (ar->start[i] == 0 || ar->end[i] == 0)
+	    /* F2018:R937:
+	     * allocate-coshape-spec is [ lower-bound-expr : ] upper-bound-expr
+	     */
+	    if (ar->start[i] == 0 || ar->end[i] == 0 || ar->stride[i] != NULL)
 	      {
-		/* If ar->stride[i] is NULL, we issued a previous error.  */
-		if (ar->stride[i] == NULL)
-		  gfc_error ("Bad array specification in ALLOCATE statement "
-			     "at %L", &e->where);
+		gfc_error ("Bad coarray specification in ALLOCATE statement "
+			   "at %L", &e->where);
 		goto failure;
 	      }
 	    else if (gfc_dep_compare_expr (ar->start[i], ar->end[i]) == 1)
diff --git a/gcc/testsuite/gfortran.dg/coarray_44.f90 b/gcc/testsuite/gfortran.dg/coarray_44.f90
index 15fb8c76ce4..545b5462251 100644
--- a/gcc/testsuite/gfortran.dg/coarray_44.f90
+++ b/gcc/testsuite/gfortran.dg/coarray_44.f90
@@ -10,3 +10,5 @@ program pr70071
   allocate (z(2)[1::2,*])  ! { dg-error "Bad array dimension" }
   allocate (z(1::2)[2,*])  ! { dg-error "Bad array specification in ALLOCATE" }
 end program pr70071
+
+! { dg-prune-output "Bad coarray specification in ALLOCATE statement" }
diff --git a/gcc/testsuite/gfortran.dg/coarray_allocate_11.f90 b/gcc/testsuite/gfortran.dg/coarray_allocate_11.f90
index 0e806f0955b..0e4f64e1e3d 100644
--- a/gcc/testsuite/gfortran.dg/coarray_allocate_11.f90
+++ b/gcc/testsuite/gfortran.dg/coarray_allocate_11.f90
@@ -3,10 +3,10 @@
 program p
    integer, allocatable :: z[:,:]
    integer :: i
-   allocate (z[1:,*]) ! { dg-error "Bad array specification in ALLOCATE statement" }
-   allocate (z[:2,*]) ! { dg-error "Bad array specification in ALLOCATE statement" }
+   allocate (z[1:,*]) ! { dg-error "Bad coarray specification in ALLOCATE statement" }
+   allocate (z[:2,*]) ! { dg-error "Bad coarray specification in ALLOCATE statement" }
    allocate (z[2:1,*]) ! { dg-error "Upper cobound is less than lower cobound" }
-   allocate (z[:0,*]) ! { dg-error "Bad array specification in ALLOCATE statement" }
+   allocate (z[:0,*]) ! { dg-error "Bad coarray specification in ALLOCATE statement" }
    allocate (z[0,*]) ! { dg-error "Upper cobound is less than lower cobound" }
    allocate (z[1,*]) ! This is OK
    allocate (z[1:1,*]) ! This is OK
diff --git a/gcc/testsuite/gfortran.dg/coarray_allocate_12.f90 b/gcc/testsuite/gfortran.dg/coarray_allocate_12.f90
new file mode 100644
index 00000000000..2169aa19998
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/coarray_allocate_12.f90
@@ -0,0 +1,19 @@
+! { dg-do compile }
+! { dg-options "-fcoarray=single" }
+!
+! PR fortran/105184
+! Based on testcases by Gerhard Steinmetz
+
+program p
+  real, allocatable :: x[:,:]
+  integer :: n = 2
+  allocate (x[  n, *])
+  allocate (x[1:n, *])
+  allocate (x[n:n, *])
+  allocate (x[n, 5:*])
+  allocate (x[ :n,   *]) ! { dg-error "Bad coarray specification" }
+  allocate (x[::n,   *]) ! { dg-error "Bad coarray specification" }
+  allocate (x[ :1:1, *]) ! { dg-error "Bad coarray specification" }
+  allocate (x[1:n:n, *]) ! { dg-error "Bad coarray specification" }
+  allocate (x[1,   : *]) ! { dg-error "Missing lower bound" }
+end
-- 
2.34.1


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

* Re: [PATCH, v2] PR fortran/105184 - ICE in gfc_array_init_size, at fortran/trans-array.cc:5841
  2022-04-08 20:44 ` [PATCH, v2] " Harald Anlauf
  2022-04-08 20:44   ` Harald Anlauf
@ 2022-04-10  7:45   ` Paul Richard Thomas
  2022-04-10  8:07   ` Thomas Koenig
  2 siblings, 0 replies; 5+ messages in thread
From: Paul Richard Thomas @ 2022-04-10  7:45 UTC (permalink / raw)
  To: Harald Anlauf; +Cc: fortran, gcc-patches

Hi Harald,

It looks good to me - OK for mainline.

Thanks

Paul


On Fri, 8 Apr 2022 at 21:45, Harald Anlauf via Fortran <fortran@gcc.gnu.org>
wrote:

> Dear all,
>
> Am 06.04.22 um 22:30 schrieb Harald Anlauf via Fortran:
> > Dear all,
> >
> > the logic for checking the allocate-coshape-spec in an ALLOCATE
> > statement was sort of sideways, and allowed to pass invalid
> > specifications to the code generation.
> >
> > The fix seems obvious (to me).
>
> after submitting the previous patch, I found another invalid case with
> a missing lower bound which was still silently accepted.  The attached
> revised patch adds this check, improves the original error message to
> actually point to the coarray specification, and renames the testcase
> to align better with existing coarray testcases.
>
> > Regtested on x86_64-pc-linux-gnu.  OK for mainline?
> > (12 or wait for 13?).
>
> Regtested again with no new failures.  OK for mainline?
>
> Thanks,
> Harald
>


-- 
"If you can't explain it simply, you don't understand it well enough" -
Albert Einstein

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

* Re: [PATCH, v2] PR fortran/105184 - ICE in gfc_array_init_size, at fortran/trans-array.cc:5841
  2022-04-08 20:44 ` [PATCH, v2] " Harald Anlauf
  2022-04-08 20:44   ` Harald Anlauf
  2022-04-10  7:45   ` Paul Richard Thomas
@ 2022-04-10  8:07   ` Thomas Koenig
  2 siblings, 0 replies; 5+ messages in thread
From: Thomas Koenig @ 2022-04-10  8:07 UTC (permalink / raw)
  To: Harald Anlauf, fortran, gcc-patches


Hi Harald,

> Regtested again with no new failures.  OK for mainline?

Looks good to me.  Thanks for the patch!

Best regards

	Thomas

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

end of thread, other threads:[~2022-04-10  8:07 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-06 20:30 [PATCH] PR fortran/105184 - ICE in gfc_array_init_size, at fortran/trans-array.cc:5841 Harald Anlauf
2022-04-08 20:44 ` [PATCH, v2] " Harald Anlauf
2022-04-08 20:44   ` Harald Anlauf
2022-04-10  7:45   ` Paul Richard Thomas
2022-04-10  8:07   ` Thomas Koenig

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).