From patchwork Thu Aug 24 21:28:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harald Anlauf X-Patchwork-Id: 136883 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a7d1:0:b0:3f2:4152:657d with SMTP id p17csp1402126vqm; Thu, 24 Aug 2023 14:28:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHj6TnMnil3XVxc02kiiPaXPnYqlebu2ZooySXYFefQqllZjN1iaO1Be4Q8a9Pz46VgcO0L X-Received: by 2002:aa7:d6d8:0:b0:522:d801:7d07 with SMTP id x24-20020aa7d6d8000000b00522d8017d07mr18501890edr.10.1692912536357; Thu, 24 Aug 2023 14:28:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692912536; cv=none; d=google.com; s=arc-20160816; b=NX7MCZeYPLjUTTwY4TrjGONcNY6mnbTNXYQ96Hx7Bp3NJBisKlEgPupkC4+EgHedJ6 ImMqQH3V0OUTj03x4lEjaWCkK17CgEKwlnsXsxgemiXrGwbKWVJk/PZFsNsaJnrTd7Ms FfmGv1ktGBGZ7MYI9oJ6+bhG+mAd0YcF3XEEozNCDtOjVaue6TGF5Moz/WdcrA5+LM0X Nqe3gYJd3leYtx2QOtIabk3mexYG40Zgm6cdzUT5bjoEwMLheO89m+ZjdlDOJglfbe7P Ivgu7zSQQs1tcAeUJiXxhANXNkF6Z51u0P/e0qpHyniv3oA68koz9LtKbuGmS0jGKq4n V9eQ== 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=E2JsqjifO9RLSJYHMzoBMubHm5d5Zd9efFqI7KbWvOY=; fh=+IEfvAe+9BRgPHWhQEl2uIBTtAiiGDh1ExRZeB5JJoc=; b=kyWxYe9sMQlW/3/NdPEwKHSNcGiGYoXE1UWpy81vo3WFE05VZT6oAOZDDoy4Xpphvx WDvUQS+GAFrufEYfdpyrKGHlAM17Om0nw4Cb2+SJMTDkso2NE4IZzDQFhuuT5MLYLkk8 Toz85G2zo3DY0NQBdPJXOrefk9MYNg0Dhf8yU5XDqe0DTdQR3X13v6Vr/gYzOVDfvksn SU/zQKk44f4a4+jhPRLG58Iz+FtJjBLD6cBr63xFxX08ajGEafVcllPYHD2Nlw8/iwNP vBD+kUWL8YoHsSH75ShYZf2zyrykjzhrdD5+2e6VmG4o/4c/lHq11Bg7Ul34J+W5KW6N JTXA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="E+IuDZ/4"; 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 (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id w8-20020aa7d288000000b0052239041251si246985edq.553.2023.08.24.14.28.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Aug 2023 14:28:56 -0700 (PDT) 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="E+IuDZ/4"; 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 0AC7E3858C2B for ; Thu, 24 Aug 2023 21:28:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0AC7E3858C2B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1692912535; bh=E2JsqjifO9RLSJYHMzoBMubHm5d5Zd9efFqI7KbWvOY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=E+IuDZ/4vK/tPoWMQkuMZ1fW5p/3533V5dFDDx1bdsAYOC4f2PUxKGmdxo00d3SgF oKAGHgnwtW+o44iZ81jH/9vkV/ZBsZGKEKCn8GjMRJhP7VamI7Ftdsj+3HM2kjAhh6 R7hg+TpVmHdK8fLWfQHnveYpkj1VIcN76VGg4+vQ= 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 919243858C53; Thu, 24 Aug 2023 21:28:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 919243858C53 X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [93.207.87.92] ([93.207.87.92]) by web-mail.gmx.net (3c-app-gmx-bap48.server.lan [172.19.172.118]) (via HTTP); Thu, 24 Aug 2023 23:28:10 +0200 MIME-Version: 1.0 Message-ID: To: fortran , gcc-patches Subject: [PATCH] Fortran: improve bounds checking for DATA with implied-do [PR35095] Date: Thu, 24 Aug 2023 23:28:10 +0200 Importance: normal Sensitivity: Normal X-Priority: 3 X-Provags-ID: V03:K1:0IZjSAXeBFpExRGX/uZLVelC+WdMLkLmQ4YOeg7XGw25pRcfji0+OYWoNCtqUtSpHzWjG vrA+2qZy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EkK7snmgUAKWxpcfhnM 2GCAw3t1CXpi6AifhUzMKIwg== X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, 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: INBOX X-GMAIL-THRID: 1775147455679775655 X-GMAIL-MSGID: 1775147455679775655 Dear all, the attached patch adds stricter bounds-checking for DATA statements with implied-do. I chose to allow overindexing (for arrays of rank greater than 1) for -std=legacy, as there might be codes in the wild that need this (and this is accepted by some other compilers, while NAG is strict here). We now get a warning with -std=gnu, and an error with -std=fxxxx. Regtested on x86_64-pc-linux-gnu. OK for mainline? (The PR is over 15 years old, so no backport intended... ;-) Thanks, Harald From 420804e7399dbc307a80f084cfb840444b8ebfe7 Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Thu, 24 Aug 2023 23:16:25 +0200 Subject: [PATCH] Fortran: improve bounds checking for DATA with implied-do [PR35095] gcc/fortran/ChangeLog: PR fortran/35095 * data.cc (get_array_index): Add bounds-checking code and return error status. Overindexing will be allowed as an extension for -std=legacy and generate an error in standard-conforming mode. (gfc_assign_data_value): Use error status from get_array_index for graceful error recovery. gcc/testsuite/ChangeLog: PR fortran/35095 * gfortran.dg/data_bounds_1.f90: Adjust options to disable warnings. * gfortran.dg/data_bounds_2.f90: New test. --- gcc/fortran/data.cc | 47 ++++++++++++++++++--- gcc/testsuite/gfortran.dg/data_bounds_1.f90 | 2 +- gcc/testsuite/gfortran.dg/data_bounds_2.f90 | 9 ++++ 3 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/data_bounds_2.f90 diff --git a/gcc/fortran/data.cc b/gcc/fortran/data.cc index 7c2537dd3f0..0589fc3906f 100644 --- a/gcc/fortran/data.cc +++ b/gcc/fortran/data.cc @@ -43,13 +43,14 @@ static void formalize_init_expr (gfc_expr *); /* Calculate the array element offset. */ -static void +static bool get_array_index (gfc_array_ref *ar, mpz_t *offset) { gfc_expr *e; int i; mpz_t delta; mpz_t tmp; + bool ok = true; mpz_init (tmp); mpz_set_si (*offset, 0); @@ -59,13 +60,42 @@ get_array_index (gfc_array_ref *ar, mpz_t *offset) e = gfc_copy_expr (ar->start[i]); gfc_simplify_expr (e, 1); - if ((gfc_is_constant_expr (ar->as->lower[i]) == 0) - || (gfc_is_constant_expr (ar->as->upper[i]) == 0) - || (gfc_is_constant_expr (e) == 0)) - gfc_error ("non-constant array in DATA statement %L", &ar->where); + if (!gfc_is_constant_expr (ar->as->lower[i]) + || !gfc_is_constant_expr (ar->as->upper[i]) + || !gfc_is_constant_expr (e)) + { + gfc_error ("non-constant array in DATA statement %L", &ar->where); + ok = false; + break; + } mpz_set (tmp, e->value.integer); gfc_free_expr (e); + + /* Overindexing is only allowed as a legacy extension. */ + if (mpz_cmp (tmp, ar->as->lower[i]->value.integer) < 0 + && !gfc_notify_std (GFC_STD_LEGACY, + "Subscript at %L below array lower bound " + "(%ld < %ld) in dimension %d", &ar->c_where[i], + mpz_get_si (tmp), + mpz_get_si (ar->as->lower[i]->value.integer), + i+1)) + { + ok = false; + break; + } + if (mpz_cmp (tmp, ar->as->upper[i]->value.integer) > 0 + && !gfc_notify_std (GFC_STD_LEGACY, + "Subscript at %L above array upper bound " + "(%ld > %ld) in dimension %d", &ar->c_where[i], + mpz_get_si (tmp), + mpz_get_si (ar->as->upper[i]->value.integer), + i+1)) + { + ok = false; + break; + } + mpz_sub (tmp, tmp, ar->as->lower[i]->value.integer); mpz_mul (tmp, tmp, delta); mpz_add (*offset, tmp, *offset); @@ -77,6 +107,8 @@ get_array_index (gfc_array_ref *ar, mpz_t *offset) } mpz_clear (delta); mpz_clear (tmp); + + return ok; } /* Find if there is a constructor which component is equal to COM. @@ -298,7 +330,10 @@ gfc_assign_data_value (gfc_expr *lvalue, gfc_expr *rvalue, mpz_t index, } if (ref->u.ar.type == AR_ELEMENT) - get_array_index (&ref->u.ar, &offset); + { + if (!get_array_index (&ref->u.ar, &offset)) + goto abort; + } else mpz_set (offset, index); diff --git a/gcc/testsuite/gfortran.dg/data_bounds_1.f90 b/gcc/testsuite/gfortran.dg/data_bounds_1.f90 index 24cdc7c9815..1e6321a2884 100644 --- a/gcc/testsuite/gfortran.dg/data_bounds_1.f90 +++ b/gcc/testsuite/gfortran.dg/data_bounds_1.f90 @@ -1,5 +1,5 @@ ! { dg-do compile } -! { dg-options "-std=gnu" } +! { dg-options "-std=gnu -w" } ! Checks the fix for PR32315, in which the bounds checks below were not being done. ! ! Contributed by Tobias Burnus diff --git a/gcc/testsuite/gfortran.dg/data_bounds_2.f90 b/gcc/testsuite/gfortran.dg/data_bounds_2.f90 new file mode 100644 index 00000000000..1aa9fd4c423 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/data_bounds_2.f90 @@ -0,0 +1,9 @@ +! { dg-do compile } +! { dg-options "-std=f2018" } +! PR fortran/35095 - Improve bounds checking for DATA with implied-do + +program chkdata + character(len=2), dimension(2,2) :: str + data (str(i,1),i=1,3) / 'A','B','C' / ! { dg-error "above array upper bound" } + data (str(j,2),j=0,2) / 'A','B','C' / ! { dg-error "below array lower bound" } +end program chkdata -- 2.35.3