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 r12-4415] Fortran: generate error message for negative elements in SHAPE array
Date: Thu, 14 Oct 2021 18:20:07 +0000 (GMT)	[thread overview]
Message-ID: <20211014182007.A552A3858412@sourceware.org> (raw)

https://gcc.gnu.org/g:b47490c572c5938f887b54240af6096a7c90f640

commit r12-4415-gb47490c572c5938f887b54240af6096a7c90f640
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Thu Oct 14 20:19:50 2021 +0200

    Fortran: generate error message for negative elements in SHAPE array
    
    gcc/fortran/ChangeLog:
    
            PR fortran/102717
            * simplify.c (gfc_simplify_reshape): Replace assert by error
            message for negative elements in SHAPE array.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/102717
            * gfortran.dg/reshape_shape_2.f90: New test.

Diff:
---
 gcc/fortran/simplify.c                        | 8 +++++++-
 gcc/testsuite/gfortran.dg/reshape_shape_2.f90 | 7 +++++++
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/gcc/fortran/simplify.c b/gcc/fortran/simplify.c
index f40e4930b58..d675f2c3aef 100644
--- a/gcc/fortran/simplify.c
+++ b/gcc/fortran/simplify.c
@@ -6840,7 +6840,13 @@ gfc_simplify_reshape (gfc_expr *source, gfc_expr *shape_exp,
       gfc_extract_int (e, &shape[rank]);
 
       gcc_assert (rank >= 0 && rank < GFC_MAX_DIMENSIONS);
-      gcc_assert (shape[rank] >= 0);
+      if (shape[rank] < 0)
+	{
+	  gfc_error ("The SHAPE array for the RESHAPE intrinsic at %L has a "
+		     "negative value %d for dimension %d",
+		     &shape_exp->where, shape[rank], rank+1);
+	  return &gfc_bad_expr;
+	}
 
       rank++;
     }
diff --git a/gcc/testsuite/gfortran.dg/reshape_shape_2.f90 b/gcc/testsuite/gfortran.dg/reshape_shape_2.f90
new file mode 100644
index 00000000000..8f1757687bc
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/reshape_shape_2.f90
@@ -0,0 +1,7 @@
+! { dg-do compile }
+! PR fortran/102717
+
+program p
+  integer, parameter :: a(1) = 2
+  integer, parameter :: b(2) = reshape([3,4], -[a]) ! { dg-error "negative" }
+end


                 reply	other threads:[~2021-10-14 18:20 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=20211014182007.A552A3858412@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).