From patchwork Wed May 24 19:16:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harald Anlauf X-Patchwork-Id: 98662 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp127485vqr; Wed, 24 May 2023 12:16:52 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4fCTjunPrq2GQSbSGSoeyS5v354wJgE5PCin6REn5xvoR7+kO6Gd/EaLL1Cq/eO++W+Z4I X-Received: by 2002:aa7:c2d8:0:b0:50d:bb87:247c with SMTP id m24-20020aa7c2d8000000b0050dbb87247cmr2540263edp.1.1684955811806; Wed, 24 May 2023 12:16:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684955811; cv=none; d=google.com; s=arc-20160816; b=AAsuBQIQmK52/zjmfTZF0A23ITLcVnqZDTBmkwGJkbezpISqNAZ3AW0HpSAnARKmjY 98lwv5P8TTs02I6dXI9egoJnOgCcfxiE06N65UBWV6qvcWpAVliegPi2X0kcqmi8wDOk DwzdcmeKYS7dDa3+a/9wRWhiYCIpY2DJzt4DcZ29u7j5LH6HONgQtDM42H9JKtjy+R6j C+/uzHO5JiKTukd61KxWYDR3VxJ91eXK1R2Gzrp2REGHbeUDYP97XmJWDmKLTeDyVsbu Gfhuxa51/paSlXrPY6SNtFAn3TN0dcy02M/7JBCXUFaoWAxsHB1BUyu27On7pNnDUSLC CF7Q== 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=/q+UwiIMv6Co3+39Os08TH6Bz6cMF7rLBmRoptRvMpo=; b=BTVKupYiMik6myqztid+wmbH8Jn7nB4+jHM5B1z5620h87iZBJTp0cbyTfr/71zXtC u/zk33BUE6nsKvFWQJy2GC/Q3Q0gyJ0S5mF+ii2JQS/NjSto8xiOqIKuCWMQM/hJlFy6 yU1yVx7hHAknRsIUBRxmq3r+WQlYEBvWF7kpFIKEGe4V1C/ib3DSysIAmnPiXS0akCG2 YufFyhIiY1gCb66QlfRxHaxt6iBz5rc9Un7t3i/kfyf9tY6u1+d+gSju+ZYh6/PKSLew 3kEB9KV7+D0Slg7HrPYJZiF9uPiGxvZsZdt5tRWYnkR/EXh0v7WoKUtMFpdGAVc9cSY+ 7B7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="AxYdFo/0"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id y6-20020a50e606000000b0050c064800a9si300017edm.100.2023.05.24.12.16.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 May 2023 12:16:51 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="AxYdFo/0"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 B2BE93858436 for ; Wed, 24 May 2023 19:16:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B2BE93858436 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1684955810; bh=/q+UwiIMv6Co3+39Os08TH6Bz6cMF7rLBmRoptRvMpo=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=AxYdFo/0wmWDKT3+uXPgVfItoobkj7a6cTcobtRcvzdrWVDYUnwpYN56ZGH3rNY7f WqEjfcSVEjRi/dxkDkbzIkKy/Q6iMXjpzhZRjwyvFZpf4alxO1256tJljkjGO6eVXi ESTT0NHfOPyBJvqpx0v+m8lcVTL4Nq9xRB/4Jz7U= 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.20]) by sourceware.org (Postfix) with ESMTPS id E985C3858D28; Wed, 24 May 2023 19:16:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E985C3858D28 X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [79.232.146.251] ([79.232.146.251]) by web-mail.gmx.net (3c-app-gmx-bap32.server.lan [172.19.172.102]) (via HTTP); Wed, 24 May 2023 21:16:02 +0200 MIME-Version: 1.0 Message-ID: To: fortran , gcc-patches Subject: [PATCH] Fortran: reject bad DIM argument of SIZE intrinsic in simplification [PR104350] Date: Wed, 24 May 2023 21:16:02 +0200 Importance: normal Sensitivity: Normal X-Priority: 3 X-Provags-ID: V03:K1:3sQyavSKNHVdm0JEdRI2TjTukbKThWLklCoIfcLseIuIU2/Kjijo3NwKJUmv+6C0Gd2lh pYRWA22uQyX8TPwybN2a2EpFiIefxeCH+qzhHbKnTXFM2We3h6z4WQKX03dMfqPCkgb/JZHwBnuy rOQmIQTy/oDn80TXgAkLGcuetMfiq1OddRW1CRK0MU4r+xe59stDSHRwHk1QXu8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ID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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?1766804225255338481?= X-GMAIL-MSGID: =?utf-8?q?1766804225255338481?= Dear all, the attached almost obvious patch fixes an ICE on invalid that may occur when we attempt to simplify an initialization expression with SIZE for an out-of-range DIM argument. Returning gfc_bad_expr allows for a more graceful error recovery. Regtested on x86_64-pc-linux-gnu. OK for mainline? Thanks, Harald From 738bdcce46bd760fcafd1eb56700c8824621266f Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Wed, 24 May 2023 21:04:43 +0200 Subject: [PATCH] Fortran: reject bad DIM argument of SIZE intrinsic in simplification [PR104350] gcc/fortran/ChangeLog: PR fortran/104350 * simplify.cc (simplify_size): Reject DIM argument of intrinsic SIZE with error when out of valid range. gcc/testsuite/ChangeLog: PR fortran/104350 * gfortran.dg/size_dim_2.f90: New test. --- gcc/fortran/simplify.cc | 12 +++++++++++- gcc/testsuite/gfortran.dg/size_dim_2.f90 | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gfortran.dg/size_dim_2.f90 diff --git a/gcc/fortran/simplify.cc b/gcc/fortran/simplify.cc index 3f77203e62e..81680117f70 100644 --- a/gcc/fortran/simplify.cc +++ b/gcc/fortran/simplify.cc @@ -7594,7 +7594,17 @@ simplify_size (gfc_expr *array, gfc_expr *dim, int k) if (dim->expr_type != EXPR_CONSTANT) return NULL; - d = mpz_get_ui (dim->value.integer) - 1; + if (array->rank == -1) + return NULL; + + d = mpz_get_si (dim->value.integer) - 1; + if (d < 0 || d > array->rank - 1) + { + gfc_error ("DIM argument (%d) to intrinsic SIZE at %L out of range " + "(1:%d)", d+1, &array->where, array->rank); + return &gfc_bad_expr; + } + if (!gfc_array_dimen_size (array, d, &size)) return NULL; } diff --git a/gcc/testsuite/gfortran.dg/size_dim_2.f90 b/gcc/testsuite/gfortran.dg/size_dim_2.f90 new file mode 100644 index 00000000000..27a71d90a47 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/size_dim_2.f90 @@ -0,0 +1,19 @@ +! { dg-do compile } +! PR fortran/104350 - ICE with SIZE and bad DIM in initialization expression +! Contributed by G. Steinmetz + +program p + implicit none + integer :: k + integer, parameter :: x(2,3) = 42 + integer, parameter :: s(*) = [(size(x,dim=k),k=1,rank(x))] + integer, parameter :: t(*) = [(size(x,dim=k),k=1,3)] ! { dg-error "out of range" } + integer, parameter :: u(*) = [(size(x,dim=k),k=0,3)] ! { dg-error "out of range" } + integer, parameter :: v = product(shape(x)) + integer, parameter :: w = product([(size(x,k),k=0,3)]) ! { dg-error "out of range" } + print *, ([(size(x,dim=k),k=1,rank(x))]) + print *, [(size(x,dim=k),k=1,rank(x))] + print *, [(size(x,dim=k),k=0,rank(x))] + print *, product([(size(x,dim=k),k=1,rank(x))]) + print *, product([(size(x,dim=k),k=0,rank(x))]) +end -- 2.35.3