]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: perform array subscript checks only for valid INTEGER bounds
authorHarald Anlauf <anlauf@gmx.de>
Tue, 7 Dec 2021 20:34:31 +0000 (21:34 +0100)
committerHarald Anlauf <anlauf@gmx.de>
Fri, 10 Dec 2021 18:26:12 +0000 (19:26 +0100)
gcc/fortran/ChangeLog:

PR fortran/103607
* frontend-passes.c (do_subscript): Ensure that array bounds are
of type INTEGER before performing checks on array subscripts.

gcc/testsuite/ChangeLog:

PR fortran/103607
* gfortran.dg/pr103607.f90: New test.

(cherry picked from commit 9eec77c0df9e5c67454a2e8f83246104458ba4f0)

gcc/fortran/frontend-passes.c
gcc/testsuite/gfortran.dg/pr103607.f90 [new file with mode: 0644]

index 706cbe1e0e658f51268118dd4f5ed4dae661aa86..de9eff826302e4f50940017ed7c233c21b0a26d8 100644 (file)
@@ -2677,6 +2677,7 @@ do_subscript (gfc_expr **e)
                    {
                      if (ar->as->lower[i]
                          && ar->as->lower[i]->expr_type == EXPR_CONSTANT
+                         && ar->as->lower[i]->ts.type == BT_INTEGER
                          && mpz_cmp (val, ar->as->lower[i]->value.integer) < 0)
                        gfc_warning (warn, "Array reference at %L out of bounds "
                                     "(%ld < %ld) in loop beginning at %L",
@@ -2686,6 +2687,7 @@ do_subscript (gfc_expr **e)
 
                      if (ar->as->upper[i]
                          && ar->as->upper[i]->expr_type == EXPR_CONSTANT
+                         && ar->as->upper[i]->ts.type == BT_INTEGER
                          && mpz_cmp (val, ar->as->upper[i]->value.integer) > 0)
                            gfc_warning (warn, "Array reference at %L out of bounds "
                                         "(%ld > %ld) in loop beginning at %L",
@@ -2701,6 +2703,7 @@ do_subscript (gfc_expr **e)
                    {
                      if (ar->as->lower[i]
                          && ar->as->lower[i]->expr_type == EXPR_CONSTANT
+                         && ar->as->lower[i]->ts.type == BT_INTEGER
                          && mpz_cmp (val, ar->as->lower[i]->value.integer) < 0)
                        gfc_warning (warn, "Array reference at %L out of bounds "
                                     "(%ld < %ld) in loop beginning at %L",
@@ -2710,6 +2713,7 @@ do_subscript (gfc_expr **e)
 
                      if (ar->as->upper[i]
                          && ar->as->upper[i]->expr_type == EXPR_CONSTANT
+                         && ar->as->upper[i]->ts.type == BT_INTEGER
                          && mpz_cmp (val, ar->as->upper[i]->value.integer) > 0)
                        gfc_warning (warn, "Array reference at %L out of bounds "
                                     "(%ld > %ld) in loop beginning at %L",
diff --git a/gcc/testsuite/gfortran.dg/pr103607.f90 b/gcc/testsuite/gfortran.dg/pr103607.f90
new file mode 100644 (file)
index 0000000..a6a2c4f
--- /dev/null
@@ -0,0 +1,12 @@
+! { dg-do compile }
+! PR fortran/103607 - ICE in do_subscript, at fortran/frontend-passes.c:2927
+! Contributed by G.Steinmetz
+
+program p
+  integer :: i, x(abs(2.)) ! { dg-error "must be of INTEGER type" }
+  do i = 1, 2
+     x(i) = 0
+  end do
+end
+
+! { dg-prune-output "must have constant shape" }