public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Nathan Sidwell <nathan@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc/devel/c++-modules] This patch fixes PR96624.
Date: Fri, 28 Aug 2020 16:09:37 +0000 (GMT)	[thread overview]
Message-ID: <20200828160937.28F4A3950C48@sourceware.org> (raw)

https://gcc.gnu.org/g:9d463ce7f983f03f0d65da03cfa430e29a0840c2

commit 9d463ce7f983f03f0d65da03cfa430e29a0840c2
Author: Paul Thomas <pault@gcc.gnu.org>
Date:   Fri Aug 28 09:02:58 2020 +0100

    This patch fixes PR96624.
    
    2020-08-28  Paul Thomas  <pault@gcc.gnu.org>
    
    gcc/fortran
            PR fortran/96624
            * simplify.c (gfc_simplify_reshape): Detect zero shape and
            clear index if found.
    
    gcc/testsuite/
            PR fortran/96624
            * gfortran.dg/reshape_8.f90 : New test.

Diff:
---
 gcc/fortran/simplify.c                  | 14 ++++++++++++--
 gcc/testsuite/gfortran.dg/reshape_8.f90 | 14 ++++++++++++++
 2 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/gcc/fortran/simplify.c b/gcc/fortran/simplify.c
index 074b50c2e68..8e0d2f97a60 100644
--- a/gcc/fortran/simplify.c
+++ b/gcc/fortran/simplify.c
@@ -6398,7 +6398,7 @@ gfc_simplify_is_contiguous (gfc_expr *array)
 
   if (gfc_is_not_contiguous (array))
     return gfc_get_logical_expr (gfc_default_logical_kind, &array->where, 0);
-    
+
   return NULL;
 }
 
@@ -6725,6 +6725,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)
@@ -6847,7 +6848,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)
     {
@@ -6897,6 +6905,8 @@ inc:
       break;
     }
 
+sizezero:
+
   mpz_clear (index);
 
   return result;
diff --git a/gcc/testsuite/gfortran.dg/reshape_8.f90 b/gcc/testsuite/gfortran.dg/reshape_8.f90
new file mode 100644
index 00000000000..01799ac5c19
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/reshape_8.f90
@@ -0,0 +1,14 @@
+! { dg-do compile }
+! { dg-options "-fdump-tree-original" }
+!
+! Test the fix for PR96624 in which an attempt was made to assign
+! to the zero length temporary created by reshape, resulting in a segfault.
+!
+! Contributed by Dong Shenpo  <shenpo.dong@compiler-dev.com>
+!
+program test
+  integer :: a(2,0)
+  a = reshape([1,2,3,4], [2,0])
+  print *, a
+end
+! { dg-final { scan-tree-dump-times "data" 4 "original" } }


                 reply	other threads:[~2020-08-28 16:09 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200828160937.28F4A3950C48@sourceware.org \
    --to=nathan@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).