https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97039
Bug ID: 97039 Summary: -fbounds-check misses violation with slice of array but not an element Product: gcc Version: 10.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: fortran Assignee: unassigned at gcc dot gnu.org Reporter: anthony.debeus at gmail dot com Target Milestone: --- Using built-in specs. COLLECT_GCC=gfortran COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++,d --with-isl --with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit --enable-cet=auto --enable-checking=release --enable-clocale=gnu --enable-default-pie --enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object --enable-install-libiberty --enable-linker-build-id --enable-lto --enable-multilib --enable-plugin --enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch --disable-libunwind-exceptions --disable-werror gdc_include_dir=/usr/include/dlang/gdc Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 10.2.0 (GCC) gfortran -Wall -Wextra -fbounds-check -fno-strict-aliasing -fwrapv -fno-aggressive-loop-optimizations -fsanitize=undefined arrays.f90 test.f90 or just gfortran -fbounds-check arrays.f90 test.f90 test.f90 PROGRAM test USE arrays call init_mat(MM,N,RM) write(*,*) RM%r(257,:) ! this prints out the out-of-bounds slice INCORRECTLY write(*,*) RM%r(257,1) ! this is caught correctly (line 7 below) write(*,*) size(RM%r) END arrays.f90 MODULE arrays INTEGER, PARAMETER :: MM=180, N=22 TYPE RMatrix REAL, ALLOCATABLE :: r(:,:) END TYPE RMatrix TYPE(RMatrix) :: RM CONTAINS subroutine init_mat(MM,N,RM) ! allocate arrays INTEGER, INTENT(IN) :: MM,N TYPE(RMatrix) :: RM allocate (RM%r(MM,N)) end subroutine init_mat end module produces ./a.out 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 At line 7 of file test.f90 Fortran runtime error: Index '257' of dimension 1 of array 'rm%r' above upper bound of 180 Error termination. Backtrace: #0 0x55edbad311c8 in ??? #1 0x55edbad31394 in ??? #2 0x7fc2b3ff7151 in ??? #3 0x55edbad2f16d in ??? #4 0xffffffffffffffff in ??? the correct result from the PGI/NVIDIA (v 20.7) compiler follows pgf90 -C arrays.f90 test.f90 ./a.out 0: Subscript out of range for array rm%r (test.f90: 6) subscript=257, lower bound=1, upper bound=180, dimension=1