public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Harald Anlauf <anlauf@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r9-9875] Fortran: dimensions of an array have to be non-negative
Date: Fri, 17 Dec 2021 21:53:51 +0000 (GMT)	[thread overview]
Message-ID: <20211217215351.1327D3858022@sourceware.org> (raw)

https://gcc.gnu.org/g:1062d2b022575e2ab942236478029cd637f937a5

commit r9-9875-g1062d2b022575e2ab942236478029cd637f937a5
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Tue Dec 7 23:06:41 2021 +0100

    Fortran: dimensions of an array have to be non-negative
    
    gcc/fortran/ChangeLog:
    
            PR fortran/103610
            * array.c (spec_dimen_size): Fix simplification of SHAPE:
            dimensions must be non-negative.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/103610
            * gfortran.dg/shape_11.f90: New test.
    
    (cherry picked from commit 5f7cdea34e118776d0ccd2ff3dda0f5acab18a94)

Diff:
---
 gcc/fortran/array.c                    |  6 ++++--
 gcc/testsuite/gfortran.dg/shape_11.f90 | 16 ++++++++++++++++
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/gcc/fortran/array.c b/gcc/fortran/array.c
index 017334839ed..2fb4e792ad4 100644
--- a/gcc/fortran/array.c
+++ b/gcc/fortran/array.c
@@ -2224,8 +2224,7 @@ gfc_copy_iterator (gfc_iterator *src)
 /********* Subroutines for determining the size of an array *********/
 
 /* These are needed just to accommodate RESHAPE().  There are no
-   diagnostics here, we just return a negative number if something
-   goes wrong.  */
+   diagnostics here, we just return false if something goes wrong.  */
 
 
 /* Get the size of single dimension of an array specification.  The
@@ -2254,6 +2253,9 @@ spec_dimen_size (gfc_array_spec *as, int dimen, mpz_t *result)
 
   mpz_add_ui (*result, *result, 1);
 
+  if (mpz_cmp_si (*result, 0) < 0)
+    mpz_set_si (*result, 0);
+
   return true;
 }
 
diff --git a/gcc/testsuite/gfortran.dg/shape_11.f90 b/gcc/testsuite/gfortran.dg/shape_11.f90
new file mode 100644
index 00000000000..127d221e710
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/shape_11.f90
@@ -0,0 +1,16 @@
+! { dg-do compile }
+! { dg-options "-fdump-tree-original" }
+! PR fortran/103610 - ICE while simplifying SHAPE
+! Contributed by G.Steinmetz
+
+program p
+  integer, parameter :: a(-1) = 1
+  integer, parameter :: b(1)  = maskl(shape(a))
+  integer, parameter :: c(1)  = shape(a)
+  integer, parameter :: d(1)  = maskr(shape(a))
+  if (b(1) /= 0) stop 1
+  if (c(1) /= 0) stop 2
+  if (d(1) /= 0) stop 3
+end
+
+! { dg-final { scan-tree-dump-not "_gfortran_stop_numeric" "original" } }


                 reply	other threads:[~2021-12-17 21:53 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=20211217215351.1327D3858022@sourceware.org \
    --to=anlauf@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).