From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2071) id 61E153858C52; Sat, 4 Feb 2023 15:43:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 61E153858C52 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675525407; bh=Vhvs5DhQOdSbNR73rC+iSObBMtJrMuLezy2CHij10Eg=; h=From:To:Subject:Date:From; b=gDxHyX2yo+P7lYf7iYINnyQxNQQX5vG8EmxXgp7fWNDulFfFkwVXjZlDce/L+3Dsl MDk0PLUK4k1/xwXYkcOPEk22wuKwjq2P5boH+8Y4K6yxw4M4s1NWE9wvBcOUImLwsr G1PH+Pm17eY5XEyDqwGuIHbPGAo/BVxk02wLneCw= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Harald Anlauf To: gcc-cvs@gcc.gnu.org Subject: [gcc r11-10504] Fortran: avoid ICE on invalid array subscript triplets [PR108501] X-Act-Checkin: gcc X-Git-Author: Harald Anlauf X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: d7ec0bdfeae883d852d7c0dfc67766a3793f5892 X-Git-Newrev: 76a6f8470c8c786b271cb0d897de891fe0d4043f Message-Id: <20230204154327.61E153858C52@sourceware.org> Date: Sat, 4 Feb 2023 15:43:27 +0000 (GMT) List-Id: https://gcc.gnu.org/g:76a6f8470c8c786b271cb0d897de891fe0d4043f commit r11-10504-g76a6f8470c8c786b271cb0d897de891fe0d4043f Author: Harald Anlauf Date: Mon Jan 23 21:19:03 2023 +0100 Fortran: avoid ICE on invalid array subscript triplets [PR108501] gcc/fortran/ChangeLog: PR fortran/108501 * interface.c (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) Diff: --- gcc/fortran/interface.c | 23 ++++++++++++++++------- gcc/testsuite/gfortran.dg/pr108501.f90 | 14 ++++++++++++++ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/gcc/fortran/interface.c b/gcc/fortran/interface.c index efb1257fac2..150f3def319 100644 --- a/gcc/fortran/interface.c +++ b/gcc/fortran/interface.c @@ -2863,7 +2863,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; @@ -2871,26 +2872,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; @@ -2931,7 +2936,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 @@ -2953,7 +2960,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 index 00000000000..09ab8c9f34f --- /dev/null +++ b/gcc/testsuite/gfortran.dg/pr108501.f90 @@ -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" }