]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: avoid ICE on invalid array subscript triplets [PR108501]
authorHarald Anlauf <anlauf@gmx.de>
Mon, 23 Jan 2023 20:19:03 +0000 (21:19 +0100)
committerHarald Anlauf <anlauf@gmx.de>
Sat, 28 Jan 2023 21:27:46 +0000 (22:27 +0100)
gcc/fortran/ChangeLog:

PR fortran/108501
* interface.cc (get_expr_storage_size): Check array subscript triplets
that we actually have integer values before trying to extract with
mpz_get_si.

gcc/testsuite/ChangeLog:

PR fortran/108501
* gfortran.dg/pr108501.f90: New test.

(cherry picked from commit 771d793df1622a476e1cf8d05f0a6aee350fa56b)

gcc/fortran/interface.cc
gcc/testsuite/gfortran.dg/pr108501.f90 [new file with mode: 0644]

index 0acd78d5f2ee7058e1d04fd7ad742c23de6ab5f6..54847301aaa14f0a33d140c9d24ff69d03e2b27c 100644 (file)
@@ -2894,7 +2894,8 @@ get_expr_storage_size (gfc_expr *e)
 
            if (ref->u.ar.stride[i])
              {
-               if (ref->u.ar.stride[i]->expr_type == EXPR_CONSTANT)
+               if (ref->u.ar.stride[i]->expr_type == EXPR_CONSTANT
+                   && ref->u.ar.stride[i]->ts.type == BT_INTEGER)
                  stride = mpz_get_si (ref->u.ar.stride[i]->value.integer);
                else
                  return 0;
@@ -2902,26 +2903,30 @@ get_expr_storage_size (gfc_expr *e)
 
            if (ref->u.ar.start[i])
              {
-               if (ref->u.ar.start[i]->expr_type == EXPR_CONSTANT)
+               if (ref->u.ar.start[i]->expr_type == EXPR_CONSTANT
+                   && ref->u.ar.start[i]->ts.type == BT_INTEGER)
                  start = mpz_get_si (ref->u.ar.start[i]->value.integer);
                else
                  return 0;
              }
            else if (ref->u.ar.as->lower[i]
-                    && ref->u.ar.as->lower[i]->expr_type == EXPR_CONSTANT)
+                    && ref->u.ar.as->lower[i]->expr_type == EXPR_CONSTANT
+                    && ref->u.ar.as->lower[i]->ts.type == BT_INTEGER)
              start = mpz_get_si (ref->u.ar.as->lower[i]->value.integer);
            else
              return 0;
 
            if (ref->u.ar.end[i])
              {
-               if (ref->u.ar.end[i]->expr_type == EXPR_CONSTANT)
+               if (ref->u.ar.end[i]->expr_type == EXPR_CONSTANT
+                   && ref->u.ar.end[i]->ts.type == BT_INTEGER)
                  end = mpz_get_si (ref->u.ar.end[i]->value.integer);
                else
                  return 0;
              }
            else if (ref->u.ar.as->upper[i]
-                    && ref->u.ar.as->upper[i]->expr_type == EXPR_CONSTANT)
+                    && ref->u.ar.as->upper[i]->expr_type == EXPR_CONSTANT
+                    && ref->u.ar.as->upper[i]->ts.type == BT_INTEGER)
              end = mpz_get_si (ref->u.ar.as->upper[i]->value.integer);
            else
              return 0;
@@ -2962,7 +2967,9 @@ get_expr_storage_size (gfc_expr *e)
                  || ref->u.ar.as->upper[i] == NULL
                  || ref->u.ar.as->lower[i] == NULL
                  || ref->u.ar.as->upper[i]->expr_type != EXPR_CONSTANT
-                 || ref->u.ar.as->lower[i]->expr_type != EXPR_CONSTANT)
+                 || ref->u.ar.as->lower[i]->expr_type != EXPR_CONSTANT
+                 || ref->u.ar.as->upper[i]->ts.type != BT_INTEGER
+                 || ref->u.ar.as->lower[i]->ts.type != BT_INTEGER)
                return 0;
 
              elements
@@ -2984,7 +2991,9 @@ get_expr_storage_size (gfc_expr *e)
            {
              if (!as->upper[i] || !as->lower[i]
                  || as->upper[i]->expr_type != EXPR_CONSTANT
-                 || as->lower[i]->expr_type != EXPR_CONSTANT)
+                 || as->lower[i]->expr_type != EXPR_CONSTANT
+                 || as->upper[i]->ts.type != BT_INTEGER
+                 || as->lower[i]->ts.type != BT_INTEGER)
                return 0;
 
              elements = elements
diff --git a/gcc/testsuite/gfortran.dg/pr108501.f90 b/gcc/testsuite/gfortran.dg/pr108501.f90
new file mode 100644 (file)
index 0000000..09ab8c9
--- /dev/null
@@ -0,0 +1,14 @@
+! { dg-do compile }
+! PR fortran/108501 - ICE in get_expr_storage_size
+! Contributed by G.Steinmetz
+
+program p
+  real, parameter :: n = 2
+  real :: a(1,(n),2) ! { dg-error "must be of INTEGER type" }
+  call s(a(:,:,1))
+end
+subroutine s(x)
+  real :: x(2)
+end
+
+! { dg-prune-output "must have constant shape" }