]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: fix wrong array bounds check [PR113471]
authorHarald Anlauf <anlauf@gmx.de>
Fri, 19 Jan 2024 20:20:44 +0000 (21:20 +0100)
committerHarald Anlauf <anlauf@gmx.de>
Fri, 19 Jan 2024 20:20:44 +0000 (21:20 +0100)
gcc/fortran/ChangeLog:

PR fortran/113471
* trans-array.cc (array_bound_check_elemental): Array bounds check
shall apply here to elemental dimensions of an array section only.

gcc/testsuite/ChangeLog:

PR fortran/113471
* gfortran.dg/bounds_check_24.f90: New test.

gcc/fortran/trans-array.cc
gcc/testsuite/gfortran.dg/bounds_check_24.f90 [new file with mode: 0644]

index 26e7adaa03f15ac8bfddff17b098171b229fbf11..878a92aff18d62f159738b2cbfc6bcf6a020cf77 100644 (file)
@@ -3600,7 +3600,7 @@ array_bound_check_elemental (gfc_se * se, gfc_ss * ss, gfc_expr * expr)
              continue;
            }
 
-         if (ref->type == REF_ARRAY && ref->u.ar.dimen > 0)
+         if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
            {
              ar = &ref->u.ar;
              for (dim = 0; dim < ar->dimen; dim++)
diff --git a/gcc/testsuite/gfortran.dg/bounds_check_24.f90 b/gcc/testsuite/gfortran.dg/bounds_check_24.f90
new file mode 100644 (file)
index 0000000..d0251e8
--- /dev/null
@@ -0,0 +1,28 @@
+! { dg-do compile }
+! { dg-additional-options "-fcheck=bounds -fdump-tree-original" }
+!
+! PR fortran/113471 - wrong array bounds check
+
+program pr113471
+  implicit none
+  type t
+     integer, dimension(2) :: c1 = 0
+  end type t
+  type(t) :: cc(7), bb(7)
+  integer :: kk = 1
+
+  ! no bounds check (can be determined at compile time):
+  call foo (cc(7)% c1)
+
+  ! bounds check involving kk, but no "outside of expected range"
+  call foo (bb(kk)% c1)
+
+contains
+  subroutine foo (c)
+    integer, intent(in) :: c(:)
+  end
+end
+
+! { dg-final { scan-tree-dump-times "below lower bound" 2 "original" } }
+! { dg-final { scan-tree-dump-times "above upper bound" 2 "original" } }
+! { dg-final { scan-tree-dump-not "outside of expected range" "original" } }