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

            Bug ID: 119827
           Summary: Out of bounds check fails on substrings for upper
                    bound
           Product: gcc
           Version: 14.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: v2000kiara at gmail dot com
  Target Milestone: ---

Hi all, 

I encountered a possible failure of -fcheck=all to pick up a substring going
out of bounds for the upper index at runtime. Consider the following code:


   program substring_bug
     implicit none
     character(len=12) :: mystr
     integer           :: i

     mystr='Hello World!'
     i = 0

     ! This does raises a runtime error
     ! print *, 'MYSTR1= ', mystr(i:i+1)

     ! This raises a compilation error
     ! print *, 'MYSTR2= ', mystr(13:13)

     ! This does not raise a compilation or runtime error
     print *, 'MYSTR3= ', mystr(1:1-1)

     ! This will not raise an error either...
     print *, 'MYSTR4= ', mystr(1:i-1)      ! i-1=0

     ! ... nor does this...
     i=-1
     print *, 'MYSTR5= ', mystr(1:i)
   end program substring_bug

Running:  
  gfortran -O0 -pedantic -Wall -Wconversion -Wcharacter-truncation
-Wdo-subscript -Wsurprising -Wunused -Wextra -fno-strict-aliasing -fwrapv
-fcheck=all
  ./a.out
gives the following output 
   MYSTR3=
   MYSTR4=
   MYSTR5= 

The first two cases in the program are correctly picked up at compilation time
or runtime using -fcheck=all. The final three cases, which are out-of-bounds on
the upper index, result in an empty string being returned as seen in the
output. 
I would expect a runtime error to be raised saying something along the lines of
"substring out of bounds: upper bound of 'mystr' is larger than <something>"

Reply via email to