From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmx.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1692912490; x=1693517290; i=anlauf@gmx.de; bh=HzMU4rm34DbczeZWvzJqP8QCFepPcJ6TIlq+wd5czL8=; h=X-UI-Sender-Class:From:To:Subject:Date; b=r0zW86qVUIj+eCq3smbAfNi/SLv8OQQ2V5XWotuRoJodRK8zOSi48QEbq9stXwwfcZfp3au C9JHU6D8+NqAlSe/poy8eSvv4vGz3IBR/IPta1IPyVkFRS4bOO/2MCiCF9SP/NgFSXfK3OShi ZXqLKGAiW+tFm6M+duxmKSfkv6hGeD8gRrYsIoz60wBHtLc1oI67YlmFvJlaVpuMdmwsJGOJt qK/8UeyeQ9TJyIIFCPmdGd/rqVPgK/ppTBdHnTVmHWZCF4rL3hzITEuK0mEa1EYUmv4LnCJAh QyAc2kvSkWNvK3yqvbeqeHfAGhVHGzRyiv68i0khFP5hBrwCXEoQ== 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: From: Harald Anlauf To: fortran , gcc-patches Subject: [PATCH] Fortran: improve bounds checking for DATA with implied-do [PR35095] Content-Type: multipart/mixed; boundary=nika-039998e3-0d5b-4ec2-b767-c2b2671a5630 Date: Thu, 24 Aug 2023 23:28:10 +0200 Importance: normal Sensitivity: Normal X-Priority: 3 X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tdMOmNyplDTjMveReG5byEc1pMwkP2sIgdCkpRty01swcHm Hiux2/6HE/AnLuFinDSA5ImwM1ZWTX37l766j852JfNi5aitFtA+hmEkK7snmgUAKWxpcfhnM 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 List-Id: --nika-039998e3-0d5b-4ec2-b767-c2b2671a5630 Content-Type: text/plain; charset=UTF-8 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 --nika-039998e3-0d5b-4ec2-b767-c2b2671a5630 Content-Type: text/x-patch Content-Disposition: attachment; filename=pr35095.diff Content-Transfer-Encoding: quoted-printable =46rom 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=3Dlegacy 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. =2D-- 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 =2D-- 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 =3D true; mpz_init (tmp); mpz_set_si (*offset, 0); @@ -59,13 +60,42 @@ get_array_index (gfc_array_ref *ar, mpz_t *offset) e =3D gfc_copy_expr (ar->start[i]); gfc_simplify_expr (e, 1); - if ((gfc_is_constant_expr (ar->as->lower[i]) =3D=3D 0) - || (gfc_is_constant_expr (ar->as->upper[i]) =3D=3D 0) - || (gfc_is_constant_expr (e) =3D=3D 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 =3D 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 =3D 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 =3D 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 *rv= alue, mpz_t index, } if (ref->u.ar.type =3D=3D 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/g= fortran.dg/data_bounds_1.f90 index 24cdc7c9815..1e6321a2884 100644 =2D-- 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=3Dgnu" } +! { dg-options "-std=3Dgnu -w" } ! Checks the fix for PR32315, in which the bounds checks below were not b= eing done. ! ! Contributed by Tobias Burnus diff --git a/gcc/testsuite/gfortran.dg/data_bounds_2.f90 b/gcc/testsuite/g= fortran.dg/data_bounds_2.f90 new file mode 100644 index 00000000000..1aa9fd4c423 =2D-- /dev/null +++ b/gcc/testsuite/gfortran.dg/data_bounds_2.f90 @@ -0,0 +1,9 @@ +! { dg-do compile } +! { dg-options "-std=3Df2018" } +! PR fortran/35095 - Improve bounds checking for DATA with implied-do + +program chkdata + character(len=3D2), dimension(2,2) :: str + data (str(i,1),i=3D1,3) / 'A','B','C' / ! { dg-error "above array upper= bound" } + data (str(j,2),j=3D0,2) / 'A','B','C' / ! { dg-error "below array lower= bound" } +end program chkdata =2D- 2.35.3 --nika-039998e3-0d5b-4ec2-b767-c2b2671a5630--