From 43ec8b856a67a1b70744e5c0d50ea7fa2dd9a8ee Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Mon, 16 Oct 2023 21:02:20 +0200
Subject: [PATCH] Fortran: out of bounds access with nested implied-do IO
[PR111837]
gcc/fortran/ChangeLog:
PR fortran/111837
* frontend-passes.cc (traverse_io_block): Dependency check of loop
nest shall be triangular, not banded.
gcc/testsuite/ChangeLog:
PR fortran/111837
* gfortran.dg/implied_do_io_8.f90: New test.
---
gcc/fortran/frontend-passes.cc | 2 +-
gcc/testsuite/gfortran.dg/implied_do_io_8.f90 | 18 ++++++++++++++++++
2 files changed, 19 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/gfortran.dg/implied_do_io_8.f90
@@ -1326,7 +1326,7 @@ traverse_io_block (gfc_code *code, bool *has_reached, gfc_code *prev)
if (iters[i])
{
gfc_expr *var = iters[i]->var;
- for (int j = i - 1; j < i; j++)
+ for (int j = 0; j < i; j++)
{
if (iters[j]
&& (var_in_expr (var, iters[j]->start)
new file mode 100644
@@ -0,0 +1,18 @@
+! { dg-do run }
+! { dg-additional-options "-fcheck=bounds" }
+! PR fortran/111837 - out of bounds access with front-end optimization
+
+program implied_do_bug
+ implicit none
+ integer :: i,j,k
+ real :: arr(1,1,1)
+ integer :: ni(1)
+ ni(1) = 1
+ arr = 1
+ write(*,*) (((arr(i,j,k), i=1,ni(k)), k=1,1), j=1,1)
+ write(*,*) (((arr(i,j,k), i=1,ni(k)), j=1,1), k=1,1)
+ write(*,*) (((arr(k,i,j), i=1,ni(k)), k=1,1), j=1,1)
+ write(*,*) (((arr(k,i,j), i=1,ni(k)), j=1,1), k=1,1)
+ write(*,*) (((arr(j,k,i), i=1,ni(k)), k=1,1), j=1,1)
+ write(*,*) (((arr(j,k,i), i=1,ni(k)), j=1,1), k=1,1)
+end
--
2.35.3