From patchwork Mon Jan 23 20:34:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harald Anlauf X-Patchwork-Id: 47375 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1804064wrn; Mon, 23 Jan 2023 12:35:48 -0800 (PST) X-Google-Smtp-Source: AMrXdXu6J3XCp+MODdWUEGxMCLgIK0AJwupRysTQuxKnUfVTxgiUWtPiw7r/iV0hlTClL0h+30Mj X-Received: by 2002:a05:6402:4cd:b0:46c:8544:42be with SMTP id n13-20020a05640204cd00b0046c854442bemr26088501edw.5.1674506147904; Mon, 23 Jan 2023 12:35:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674506147; cv=none; d=google.com; s=arc-20160816; b=c3kvQMwXLMFwQWozngXITKkgKDYLJeRY0XozInT1rBLw3VDgiNplWjBr/etU909vvW PiOAOaVMMJ7RADVCmXDCAangJSlIvSuhMmW+w5yuV0przPqxGYRiyhToCM9MMt3d/lwA n+GJ4w76vySctHJHOUfK0Whwzg3H+1oOeMdZRxORHjrM25iTQmFFzby0Y3LbnWa7Y+B5 djbyOziNBRu2f2OkuLKBK8l/ouqUGPpQZYBU0iLNj0qjKtNtHw2eFMzMnpR/seqVmOjg hTfklcyOZNphz75jU004XkRnGttzeI+D/If7bQDZeYB2MpM8GLUfbXvpahGoakz62sod xJDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:ui-outboundreport :sensitivity:importance:date:subject:to:message-id:mime-version :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=mRHfgRNAUPg6c2QRIefam/92Q8fOEdW6w7Bp9V3nbHU=; b=bD4RG9AtiO4ZP8UtrKA2RQ+VGLM1Pm8IPtXbu21kSeqDw1cEx9R/6rbCcLCqvqNbAA o0hYRL6FdsPZxmkG8Dik5QC5xR9Of7WUWPk0ns9lQXIvahQIfiAiuZuIaFJm+uiWy5V8 cNL8TovZF6rLbLMV9Hfz/DTFQ6J6BL6ZwLO210rK2HP5VXbcuG0+0GD2NG5VVEzhlJOi AVVyQJnV36UmvNTkx0vQcu4w11tePj56WQIoRNBIIw502XJVEtI6zkGRqGdVRQEbI9Uk ccN5yqZjggPqZ4RPLm7VBoAXr9eJUy3jxu6FuHFnvtl85D14L7a50+RI/B68nX8iEoRp hDfg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=C3bVgfgO; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id xd2-20020a170907078200b0084d302afcc3si43583141ejb.79.2023.01.23.12.35.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Jan 2023 12:35:47 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=C3bVgfgO; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 07F8538582AC for ; Mon, 23 Jan 2023 20:35:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 07F8538582AC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1674506145; bh=mRHfgRNAUPg6c2QRIefam/92Q8fOEdW6w7Bp9V3nbHU=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=C3bVgfgO9z5kGbv6txlWs5xPbYhC08o9JMZCQGLI7nUl//kB1WSua/Xia9siT6fcd B8tRxe1QMr3Jj93oU59bgPAuBiS+qvZbr/+R6U0ANgsrvLSbfTeNo1UDZBm4r78E3F Fk8CI845nFgk7QsVsxxg86xHcLfQk9HTojR6IZDY= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout.gmx.net (mout.gmx.net [212.227.17.22]) by sourceware.org (Postfix) with ESMTPS id F36343858D33; Mon, 23 Jan 2023 20:35:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F36343858D33 X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [93.207.90.148] ([93.207.90.148]) by web-mail.gmx.net (3c-app-gmx-bap50.server.lan [172.19.172.120]) (via HTTP); Mon, 23 Jan 2023 21:34:59 +0100 MIME-Version: 1.0 Message-ID: To: fortran , gcc-patches Subject: [PATCH] Fortran: avoid ICE on invalid array subscript triplets [PR108501] Date: Mon, 23 Jan 2023 21:34:59 +0100 Importance: normal Sensitivity: Normal X-Priority: 3 X-Provags-ID: V03:K1:jOE2SjraXibIssrQ+PqbY26QTuLkksQMopqErZYROxNxI4hKgUMN5pBa97b3psJGkIENA HzYZLlLXd/ht+VhFrzscpVe/n2iO4Gc+0L1hUOQ5LD34ILxGVtjAXN3Blpb6xXtQLRfvlMs/hNXW SUXRhWGJJpRODBqeVuEDBRm7TKpKDtIDqFrNGB5NtETaSkra44r11/I1OUl1q36kRGK03UCTMBCi UfffPT96och3npM+0XH7TmUwuEKdseHgXY5kUDw0LPyImtiKMc/NNmahEWJkVoHvbnPo6Op3wjCC aQ= UI-OutboundReport: notjunk:1;M01:P0:tTW+Vi2+gs4=;s5K6CWfhoHnZRdXjHxjGGXRqg+2 CElffbu3lHrP9hkP2tdFNKxmcrupjIGERkms/un/2NBGz6jWutw/TJU6fjhThyMppuQoVV+KR TG1yQbjRzB60I/qYGGhxFK2D0UUPg1hnuwgleZKrvHvWG3XfdpoYQ2LohTfCnkwGiOxGDpFEE ngjnHYj1l4ArKfimeVKJE7ZUNI4jK4Uy2MdGiNcJ/mX91jItpmQypUvU+2EvU7OBwQ7n0WETw aH2mt/+tltn087mKwqmNxNJvLEdA76n0EalI2YchDS+lAMPGMCiKCOB8JL/2ALoOd3sG9xOux vqQE1n0ij4G/uzjQ4S1i/V223JlksDhNJtmnlq8aGoETVa7nVBgA9w28L1M+1dum7pzc36xcG cMZcLpjv+XTdbQKDD47OVw90Y8Bpzt1rudD4jodUxT2zeB4ZjZfSGONaEj2TF2Spee4FCNXvo orvf9c5Ux0SpA9AiYn07R+vMPc4Morlp47xKBTanpxJyqMImkBTlLoOAlBLAC0VdkhLrWUgfO XBWcIkqlOnSjZ6w2+tnUm4xXxx9hHEBukCHcgqA2jEk8/x5I2vjn2ReNCw+zHZABgwmZ2/F1n K84KNui6YtrsySMEB9J6AYBVPeiPupNkE0/rXi81blUwy78KDJvzzGPDG51R8tUYrGUYp7aPS JvYqDcVo6nBURZYiPOWM43JftQYlNwO5FGAY83bppTaHr5iB5DOoxJsRenYfmw8dAvg/vbJbe Wokug3O4Z/9uFQkAf47+L/U1B5uKixyJ9tauDpMpYk8t+hRTahztzi3mocJAqe6bh+YBLNJaT icvZfDYv3HQag55LYFp6gkXA== X-Spam-Status: No, score=-13.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Harald Anlauf via Gcc-patches From: Harald Anlauf Reply-To: Harald Anlauf Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755846958557298480?= X-GMAIL-MSGID: =?utf-8?q?1755846958557298480?= Dear all, we did not check array element triplets for validity strictly enough (i.e. defensively in the case of invalid code), so we could encounter non-integer constant expressions that were passed to mpz_get_si. The attached obvious patch tries to fix all such potential issues in get_expr_storage_size. Regtested on x86_64-pc-linux-gnu. OK for mainline? Thanks, Harald From 771d793df1622a476e1cf8d05f0a6aee350fa56b Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Mon, 23 Jan 2023 21:19:03 +0100 Subject: [PATCH] Fortran: avoid ICE on invalid array subscript triplets [PR108501] 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. --- gcc/fortran/interface.cc | 23 ++++++++++++++++------- gcc/testsuite/gfortran.dg/pr108501.f90 | 14 ++++++++++++++ 2 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/pr108501.f90 diff --git a/gcc/fortran/interface.cc b/gcc/fortran/interface.cc index 9593fa83c45..dafe41753b7 100644 --- a/gcc/fortran/interface.cc +++ b/gcc/fortran/interface.cc @@ -2910,7 +2910,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; @@ -2918,26 +2919,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; @@ -2978,7 +2983,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 @@ -3000,7 +3007,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" } -- 2.35.3