]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: out of bounds access with nested implied-do IO [PR111837]
authorHarald Anlauf <anlauf@gmx.de>
Mon, 16 Oct 2023 19:02:20 +0000 (21:02 +0200)
committerHarald Anlauf <anlauf@gmx.de>
Tue, 17 Oct 2023 16:55:22 +0000 (18:55 +0200)
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
gcc/testsuite/gfortran.dg/implied_do_io_8.f90 [new file with mode: 0644]

index 136a292807d3ce44a82529e7247a71abc60270d2..536884b13f09a62ee643052fb31e6b39c3961599 100644 (file)
@@ -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)
diff --git a/gcc/testsuite/gfortran.dg/implied_do_io_8.f90 b/gcc/testsuite/gfortran.dg/implied_do_io_8.f90
new file mode 100644 (file)
index 0000000..c66a0f6
--- /dev/null
@@ -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