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

            Bug ID: 119118
           Summary: substring with negative start index
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kargls at comcast dot net
  Target Milestone: ---

All,

I found the following issue when perusing the lfortran bug list.

   program main

   implicit none

   integer n
   character(len=30) str

   n = 3
   str = "123456789012345678901234567890"
   print *,  str(-1:n), len(str(-1:n))       ! Whoops
   print *,  str(-1:-2), len(str(-1:-2))     ! OK?  zero-length substring
!  print*,  str(-1:3), len(str(-1:3))        ! gfortran issues error.

   print *, myshow(n, n, str)    ! OK
   print *, myshow(-1, -2, str)  ! OK? zero-length substring
!   print *, myshow(-1, n, str)   ! gfortran issues a run-time with -fcheck=all
!   print *, myshow(-1, 3, str)   ! gfortran issues a run-time with -fcheck=all

   contains

      function myshow(i, j, s) result(t)
         character(len=:), allocatable :: t
         character(len=*), intent(in) :: s
         integer, intent(in) :: i, j
         t = s(i:j)
      end function myshow        

   end program main

% gfcx -o z -fcheck=all -O3 -Wall -Wextra a.f90
% ./z
 123           5
            0
 3

As you can see, the line marked with 'Whoops' yields a rather odd result.
Not only is the line not diagnosed with a run-time error, it produces a
string with 3 characters but with a reported length of 5.

Reply via email to