public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug fortran/98490] New: Unexpected out of bounds in array constructor with implied do loop
@ 2020-12-31 18:42 ffadrique at gmail dot com
  2020-12-31 18:53 ` [Bug fortran/98490] " dominiq at lps dot ens.fr
                   ` (18 more replies)
  0 siblings, 19 replies; 20+ messages in thread
From: ffadrique at gmail dot com @ 2020-12-31 18:42 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98490

            Bug ID: 98490
           Summary: Unexpected out of bounds in array constructor with
                    implied do loop
           Product: gcc
           Version: 9.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ffadrique at gmail dot com
  Target Milestone: ---

The followinng code, when compiled with -fbounds-check produces a runtime
exception

program test

  implicit none

  call sub( 'Lorem ipsum' )

contains

subroutine sub( text )

  character(len=*), intent(in) :: text

  integer :: i

  write(*,*) [ ( text(i:i), i = 1, len(text) ) ]

end subroutine sub

end program test

Run time error:
At line 18 of file test.f90
Fortran runtime error: Substring out of bounds: lower bound (-2147483648) of
'text' is less than one

Error termination. Backtrace:
#0  0x7f01d0965d3a
#1  0x7f01d0966849
#2  0x7f01d0966ec6
#3  0x5567acbd1269
#4  0x5567acbd14e6
#5  0x5567acbd151e
#6  0x7f01d077a0b2
#7  0x5567acbd10fd
#8  0xffffffffffffffff

It is clear to me that the automatic variable i is not initialised before
invoking the implied do loop. However, the loop should run between 1 and the
length of the input text as given in the loop controls; not evaluate at the
uninitialised value.

The behaviour is as expected when not compiled with the bound check flag. Also
valgrind is not reporting any invalid memory access. The issue may be
associated to the compilation with the bound check flag (that I need to have
enabled in general). The workaround is straightforward.

I have also checked that if there were several statements like the write one in
the example, I would have to control the initial value of i before each implied
loop to make sure that the initial value is not out of the array in the loop.

  i = 1  ! Prevents the exception
  write(*,*) [ ( text1(i:i), i = 1, len(text1) ) ]  ! text1 of length 20

! Next statement triggers the exception unless i is reset again
  write(*,*) [ ( text2(i:i), i = 1, len(text2) ) ]  ! text2 of length 10

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

end of thread, other threads:[~2022-03-18 19:48 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-31 18:42 [Bug fortran/98490] New: Unexpected out of bounds in array constructor with implied do loop ffadrique at gmail dot com
2020-12-31 18:53 ` [Bug fortran/98490] " dominiq at lps dot ens.fr
2020-12-31 19:11 ` ffadrique at gmail dot com
2020-12-31 19:48 ` kargl at gcc dot gnu.org
2020-12-31 19:49 ` kargl at gcc dot gnu.org
2021-01-02  4:08 ` jvdelisle at charter dot net
2021-01-02  4:12 ` jvdelisle at charter dot net
2021-01-02  4:24 ` sgk at troutmask dot apl.washington.edu
2021-01-02 21:03 ` sgk at troutmask dot apl.washington.edu
2021-01-25 13:40 ` pault at gcc dot gnu.org
2021-09-08 20:36 ` anlauf at gcc dot gnu.org
2021-09-08 20:43 ` kargl at gcc dot gnu.org
2021-09-09 19:34 ` cvs-commit at gcc dot gnu.org
2021-09-09 19:40 ` anlauf at gcc dot gnu.org
2021-09-11 19:03 ` cvs-commit at gcc dot gnu.org
2021-09-16 18:30 ` cvs-commit at gcc dot gnu.org
2021-09-30 19:12 ` cvs-commit at gcc dot gnu.org
2021-09-30 19:13 ` anlauf at gcc dot gnu.org
2021-11-02 18:33 ` anlauf at gcc dot gnu.org
2022-03-18 19:48 ` anlauf at gcc dot gnu.org

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