public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Fortran : ICE for division by zero in declaration PR95882
@ 2020-08-25  6:33 Mark Eggleston
  2020-08-26 13:26 ` Thomas Koenig
  0 siblings, 1 reply; 6+ messages in thread
From: Mark Eggleston @ 2020-08-25  6:33 UTC (permalink / raw)
  To: gcc-patches, fortran

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

Second attempt, this time with the correct attachment.

OK to commit and backport?

[PATCH] Fortran  : ICE for division by zero in declaration PR95882

A length expression containing a divide by zero in a character
declaration will result in an ICE if the constant is anymore
complicated that a contant divided by a constant.

The cause was that char_len_param_value can return MATCH_YES
even if a divide by zero was seen.  Prior to returning check
whether a divide by zero was seen and if so set it to MATCH_ERROR.

2020-08-24  Mark Eggleston <markeggleston@gcc.gnu.org>

gcc/fortran

     PR fortran/95882
     * decl.c (char_len_param_value): Check gfc_seen_div0 and
     it is set return MATCH_ERROR.

2020-08-24  Mark Eggleston <markeggleston@gcc.gnu.org>

gcc/testsuite/

     PR fortran/95882
     * gfortran.dg/pr95882_1.f90: New test.
     * gfortran.dg/pr95882_2.f90: New test.
     * gfortran.dg/pr95882_3.f90: New test.
     * gfortran.dg/pr95882_4.f90: New test.
     * gfortran.dg/pr95882_5.f90: New test.

-- 
https://www.codethink.co.uk/privacy.html


[-- Attachment #2: 0001-Fortran-ICE-for-division-by-zero-in-declaration-pr95.patch --]
[-- Type: text/x-patch, Size: 3774 bytes --]

From e97963ec8edc58217d2ff225c58256ebd61c8e7c Mon Sep 17 00:00:00 2001
From: Mark Eggleston <markeggleston@gcc.gnu.org>
Date: Fri, 21 Aug 2020 06:39:30 +0100
Subject: [PATCH] Fortran  : ICE for division by zero in declaration PR95882

A length expression containing a divide by zero in a character
declaration will result in an ICE if the constant is anymore
complicated that a contant divided by a constant.

The cause was that char_len_param_value can return MATCH_YES
even if a divide by zero was seen.  Prior to returning check
whether a divide by zero was seen and if so set it to MATCH_ERROR.

2020-08-24  Mark Eggleston  <markeggleston@gcc.gnu.org>

gcc/fortran

	PR fortran/95882
	* decl.c (char_len_param_value): Check gfc_seen_div0 and
	it is set return MATCH_ERROR.

2020-08-24  Mark Eggleston  <markeggleston@gcc.gnu.org>

gcc/testsuite/

	PR fortran/95882
	* gfortran.dg/pr95882_1.f90: New test.
	* gfortran.dg/pr95882_2.f90: New test.
	* gfortran.dg/pr95882_3.f90: New test.
	* gfortran.dg/pr95882_4.f90: New test.
	* gfortran.dg/pr95882_5.f90: New test.
---
 gcc/fortran/decl.c                      | 3 +++
 gcc/testsuite/gfortran.dg/pr95882_1.f90 | 8 ++++++++
 gcc/testsuite/gfortran.dg/pr95882_2.f90 | 6 ++++++
 gcc/testsuite/gfortran.dg/pr95882_3.f90 | 6 ++++++
 gcc/testsuite/gfortran.dg/pr95882_4.f90 | 7 +++++++
 gcc/testsuite/gfortran.dg/pr95882_5.f90 | 6 ++++++
 6 files changed, 36 insertions(+)
 create mode 100644 gcc/testsuite/gfortran.dg/pr95882_1.f90
 create mode 100644 gcc/testsuite/gfortran.dg/pr95882_2.f90
 create mode 100644 gcc/testsuite/gfortran.dg/pr95882_3.f90
 create mode 100644 gcc/testsuite/gfortran.dg/pr95882_4.f90
 create mode 100644 gcc/testsuite/gfortran.dg/pr95882_5.f90

diff --git a/gcc/fortran/decl.c b/gcc/fortran/decl.c
index d854b2a0307..c612b492f3e 100644
--- a/gcc/fortran/decl.c
+++ b/gcc/fortran/decl.c
@@ -1146,6 +1146,9 @@ char_len_param_value (gfc_expr **expr, bool *deferred)
       gfc_free_expr (e);
     }
 
+  if (gfc_seen_div0)
+    m = MATCH_ERROR;
+
   return m;
 
 syntax:
diff --git a/gcc/testsuite/gfortran.dg/pr95882_1.f90 b/gcc/testsuite/gfortran.dg/pr95882_1.f90
new file mode 100644
index 00000000000..c254bddf494
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr95882_1.f90
@@ -0,0 +1,8 @@
+! { dg-do compile }
+
+module m
+   type t
+      character(((0)/0)) :: c  ! { dg-error "Division by zero" }
+   end type
+end
+
diff --git a/gcc/testsuite/gfortran.dg/pr95882_2.f90 b/gcc/testsuite/gfortran.dg/pr95882_2.f90
new file mode 100644
index 00000000000..d308f0c3181
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr95882_2.f90
@@ -0,0 +1,6 @@
+! { dg-do compile }
+
+module m
+   character(0/(0)) :: c = '123456789'  ! { dg-error "Division by zero" }
+end
+
diff --git a/gcc/testsuite/gfortran.dg/pr95882_3.f90 b/gcc/testsuite/gfortran.dg/pr95882_3.f90
new file mode 100644
index 00000000000..bd849135480
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr95882_3.f90
@@ -0,0 +1,6 @@
+! { dg-do compile }
+
+subroutine s(c)
+   character(((0)/0)) :: c  ! { dg-error "Division by zero" }
+end
+
diff --git a/gcc/testsuite/gfortran.dg/pr95882_4.f90 b/gcc/testsuite/gfortran.dg/pr95882_4.f90
new file mode 100644
index 00000000000..52892d32b8b
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr95882_4.f90
@@ -0,0 +1,7 @@
+! { dg-do compile }
+
+program p
+   character(((0)/0)) :: c  ! { dg-error "Division by zero" }
+   common /x/ c
+end
+
diff --git a/gcc/testsuite/gfortran.dg/pr95882_5.f90 b/gcc/testsuite/gfortran.dg/pr95882_5.f90
new file mode 100644
index 00000000000..dcdf5304052
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr95882_5.f90
@@ -0,0 +1,6 @@
+! { dg-do compile }
+
+program p
+   character(0/(0)) :: c = '123456789'  ! { dg-error "Division by zero" }
+   common c
+end
-- 
2.11.0


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

* Re: [PATCH] Fortran : ICE for division by zero in declaration PR95882
  2020-08-25  6:33 [PATCH] Fortran : ICE for division by zero in declaration PR95882 Mark Eggleston
@ 2020-08-26 13:26 ` Thomas Koenig
  0 siblings, 0 replies; 6+ messages in thread
From: Thomas Koenig @ 2020-08-26 13:26 UTC (permalink / raw)
  To: Mark Eggleston, gcc-patches, fortran

Hi Mark,

> OK to commit and backport?

OK.  Thanks for the patch!

Best regards

	Thomas

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

* Re: [PATCH] Fortran : ICE for division by zero in declaration PR95882
  2020-08-25  6:13   ` Mark Eggleston
@ 2020-08-25  6:29     ` Mark Eggleston
  0 siblings, 0 replies; 6+ messages in thread
From: Mark Eggleston @ 2020-08-25  6:29 UTC (permalink / raw)
  To: Thomas Koenig, gcc-patches, fortran


On 25/08/2020 07:13, Mark Eggleston wrote:
>
> On 24/08/2020 17:42, Thomas Koenig wrote:
>> Hi Mark,
>>
>>> OK to commit and backport?
>>
>> The test cases mentioned in the ChangeLog are not in the
>> patch, instead there is the test case for PR 96624.
>>
>> Could you correct that?
> Whoops, yes I'll fix that.
It is actually the wrong attachment, I'll try again.
>>
>> Best regards
>>
>>     Thomas
>>
-- 
https://www.codethink.co.uk/privacy.html


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

* Re: [PATCH] Fortran : ICE for division by zero in declaration PR95882
  2020-08-24 16:42 ` Thomas Koenig
@ 2020-08-25  6:13   ` Mark Eggleston
  2020-08-25  6:29     ` Mark Eggleston
  0 siblings, 1 reply; 6+ messages in thread
From: Mark Eggleston @ 2020-08-25  6:13 UTC (permalink / raw)
  To: Thomas Koenig, gcc-patches, fortran


On 24/08/2020 17:42, Thomas Koenig wrote:
> Hi Mark,
>
>> OK to commit and backport?
>
> The test cases mentioned in the ChangeLog are not in the
> patch, instead there is the test case for PR 96624.
>
> Could you correct that?
Whoops, yes I'll fix that.
>
> Best regards
>
>     Thomas
>
-- 
https://www.codethink.co.uk/privacy.html


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

* Re: [PATCH] Fortran : ICE for division by zero in declaration PR95882
  2020-08-24  7:03 Mark Eggleston
@ 2020-08-24 16:42 ` Thomas Koenig
  2020-08-25  6:13   ` Mark Eggleston
  0 siblings, 1 reply; 6+ messages in thread
From: Thomas Koenig @ 2020-08-24 16:42 UTC (permalink / raw)
  To: Mark Eggleston, gcc-patches, fortran

Hi Mark,

> OK to commit and backport?

The test cases mentioned in the ChangeLog are not in the
patch, instead there is the test case for PR 96624.

Could you correct that?

Best regards

	Thomas

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

* [PATCH] Fortran : ICE for division by zero in declaration PR95882
@ 2020-08-24  7:03 Mark Eggleston
  2020-08-24 16:42 ` Thomas Koenig
  0 siblings, 1 reply; 6+ messages in thread
From: Mark Eggleston @ 2020-08-24  7:03 UTC (permalink / raw)
  To: gcc-patches, fortran

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

Please find attached a fix for PR95882.

Tested on x86_64 with bootstrap.

OK to commit and backport?

Fortran  : ICE for division by zero in declaration PR95882

A length expression containing a divide by zero in a character
declaration will result in an ICE if the constant is anymore
complicated that a contant divided by a constant.

The cause was that char_len_param_value can return MATCH_YES
even if a divide by zero was seen.  Prior to returning check
whether a divide by zero was seen and if so set it to MATCH_ERROR.

2020-08-24  Mark Eggleston <markeggleston@gcc.gnu.org>

gcc/fortran

     PR fortran/95882
     * decl.c (char_len_param_value): Check gfc_seen_div0 and
     if it is set return MATCH_ERROR.

2020-08-24  Mark Eggleston <markeggleston@gcc.gnu.org>

gcc/testsuite/

     PR fortran/95882
     * gfortran.dg/pr95882_1.f90: New test.
     * gfortran.dg/pr95882_2.f90: New test.
     * gfortran.dg/pr95882_3.f90: New test.
     * gfortran.dg/pr95882_4.f90: New test.
     * gfortran.dg/pr95882_5.f90: New test.

-- 
https://www.codethink.co.uk/privacy.html


[-- Attachment #2: 0001-Fortran-Runtime-error-reshape-constant-array-assignm.patch --]
[-- Type: text/x-patch, Size: 2551 bytes --]

From ab94bb744a7d64751f6b93cc56ad3ed5fe5cfc81 Mon Sep 17 00:00:00 2001
From: Mark Eggleston <markeggleston@gcc.gnu.org>
Date: Mon, 17 Aug 2020 13:50:28 +0100
Subject: [PATCH] Fortran  : Runtime error, reshape constant array assignment
 PR96624

When assigning a reshaped constant array of shape [2,0] to a
variable fails with an invalid memory access.  If a varibale
with the parameter attribute is initialised with the same reshape
there is no runtime error.

2020-08-20  Steven G. Kargl  <kargl@gcc.gnu.org>

gcc/fortran/

	PR fortran/96624
	* simplify.c (gfc_simplifiy_reshape): Add new variable "zerosize".
	Set zerosize if any of the result shape ranks are zero.  After
	setting the result shapes, if zerosize is set jump to new label
	"sizezero".  Add label "sizezero" just before clearing index and
	returning result.

2020-08-20  Mark Eggleston  <markeggleston@gcc.gnu.org>

gcc/testsuite/

	PR fortran/96624
	*gfortran/pr96624.f90: New test.
---
 gcc/fortran/simplify.c                | 11 ++++++++++-
 gcc/testsuite/gfortran.dg/pr96624.f90 | 10 ++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gfortran.dg/pr96624.f90

diff --git a/gcc/fortran/simplify.c b/gcc/fortran/simplify.c
index eb8b2afeb29..0d77d289651 100644
--- a/gcc/fortran/simplify.c
+++ b/gcc/fortran/simplify.c
@@ -6721,6 +6721,7 @@ gfc_simplify_reshape (gfc_expr *source, gfc_expr *shape_exp,
   unsigned long j;
   size_t nsource;
   gfc_expr *e, *result;
+  bool zerosize = false;
 
   /* Check that argument expression types are OK.  */
   if (!is_constant_array_expr (source)
@@ -6843,7 +6844,14 @@ gfc_simplify_reshape (gfc_expr *source, gfc_expr *shape_exp,
   result->rank = rank;
   result->shape = gfc_get_shape (rank);
   for (i = 0; i < rank; i++)
-    mpz_init_set_ui (result->shape[i], shape[i]);
+    {
+      mpz_init_set_ui (result->shape[i], shape[i]);
+      if (shape[i] == 0)
+	zerosize = true;
+    }
+ 
+  if (zerosize)
+    goto sizezero;
 
   while (nsource > 0 || npad > 0)
     {
@@ -6893,6 +6901,7 @@ inc:
       break;
     }
 
+sizezero:
   mpz_clear (index);
 
   return result;
diff --git a/gcc/testsuite/gfortran.dg/pr96624.f90 b/gcc/testsuite/gfortran.dg/pr96624.f90
new file mode 100644
index 00000000000..a4cfe5c3279
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr96624.f90
@@ -0,0 +1,10 @@
+! { dg-do run }
+
+program test
+  integer :: a(2,0)
+  character(4) :: buffer
+  a = reshape([1,2,3,4], [2,0])
+  write(buffer,"(2a1)") ">", "<"
+  if (trim(buffer).ne."><") stop 1
+end
+
-- 
2.11.0


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

end of thread, other threads:[~2020-08-26 13:26 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-25  6:33 [PATCH] Fortran : ICE for division by zero in declaration PR95882 Mark Eggleston
2020-08-26 13:26 ` Thomas Koenig
  -- strict thread matches above, loose matches on Subject: below --
2020-08-24  7:03 Mark Eggleston
2020-08-24 16:42 ` Thomas Koenig
2020-08-25  6:13   ` Mark Eggleston
2020-08-25  6:29     ` Mark Eggleston

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