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-9750] Fortran - out of bounds in array constructor with implied do loop
Date: Thu, 30 Sep 2021 19:12:26 +0000 (GMT)	[thread overview]
Message-ID: <20210930191226.C816B3858C2C@sourceware.org> (raw)

https://gcc.gnu.org/g:97a5a41dbfef1722fdaf4dc979e8b9c274b4404d

commit r9-9750-g97a5a41dbfef1722fdaf4dc979e8b9c274b4404d
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Thu Sep 9 21:34:01 2021 +0200

    Fortran - out of bounds in array constructor with implied do loop
    
    gcc/fortran/ChangeLog:
    
            PR fortran/98490
            * trans-expr.c (gfc_conv_substring): Do not generate substring
            bounds check for implied do loop index variable before it actually
            becomes defined.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/98490
            * gfortran.dg/bounds_check_23.f90: New test.
    
    (cherry picked from commit 5fe0865ab788bdc387b284a3ad57e5a95a767b18)

Diff:
---
 gcc/fortran/trans-expr.c                      |  4 +++-
 gcc/testsuite/gfortran.dg/bounds_check_23.f90 | 18 ++++++++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/gcc/fortran/trans-expr.c b/gcc/fortran/trans-expr.c
index 722149493b6..e240441ac2c 100644
--- a/gcc/fortran/trans-expr.c
+++ b/gcc/fortran/trans-expr.c
@@ -2531,7 +2531,9 @@ gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind,
   if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
     end.expr = gfc_evaluate_now (end.expr, &se->pre);
 
-  if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
+  if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
+      && (ref->u.ss.start->symtree
+	  && !ref->u.ss.start->symtree->n.sym->attr.implied_index))
     {
       tree nonempty = fold_build2_loc (input_location, LE_EXPR,
 				       logical_type_node, start.expr,
diff --git a/gcc/testsuite/gfortran.dg/bounds_check_23.f90 b/gcc/testsuite/gfortran.dg/bounds_check_23.f90
new file mode 100644
index 00000000000..8de90c77c01
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/bounds_check_23.f90
@@ -0,0 +1,18 @@
+! { dg-do run }
+! { dg-options "-fcheck=bounds -fdump-tree-original" }
+! PR fortran/98490 - out of bounds in array constructor with implied do loop
+
+program test
+  implicit none
+  call sub('Lorem ipsum')
+contains
+  subroutine sub( text )
+    character(len=*), intent(in)  :: text
+    character(len=1), allocatable :: c(:)
+    integer :: i
+    c = [ ( text(i:i), i = 1, len(text) ) ]
+    if (c(1) /= 'L') stop 1
+  end subroutine sub
+end program test
+
+! { dg-final { scan-tree-dump-times "Substring out of bounds:" 2 "original" } }


                 reply	other threads:[~2021-09-30 19:12 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=20210930191226.C816B3858C2C@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).