From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from imap2.colo.codethink.co.uk (imap2.colo.codethink.co.uk [78.40.148.184]) by sourceware.org (Postfix) with ESMTPS id 145083858D38; Thu, 20 Aug 2020 07:05:12 +0000 (GMT) Authentication-Results: sourceware.org; dmarc=permerror header.from=codethink.co.uk Authentication-Results: sourceware.org; spf=none smtp.mailfrom=mark.eggleston@codethink.co.uk Received: from [2.219.18.83] (helo=[192.168.0.9]) by imap2.colo.codethink.co.uk with esmtpsa (Exim 4.92 #3 (Debian)) id 1k8edJ-0006TU-M0; Thu, 20 Aug 2020 08:05:09 +0100 To: gcc-patches , fortran From: Mark Eggleston Subject: [PATCH] Fortran : Runtime error, reshape constant array assignment, PR96624 Message-ID: <7d52fb1c-32b6-ae74-b8b2-281149c52971@codethink.co.uk> Date: Thu, 20 Aug 2020 08:05:08 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.8.0 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------83E23468DA5C5AC70E9CBD35" Content-Language: en-US X-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_COUK, KAM_DMARC_STATUS, KAM_LAZY_DOMAIN_SECURITY, KAM_NUMSUBJECT, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: fortran@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Fortran mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 20 Aug 2020 07:05:14 -0000 This is a multi-part message in MIME format. --------------83E23468DA5C5AC70E9CBD35 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Please find attached a fix for PR96624.  The original patch was by Steve Kargl. Also occurs on releases/gcc-10, releases/gcc-9 and releases/gcc-8 branches. OK to commit to master and backport? [PATCH] Fortran  : Runtime error, reshape constant array assignment  PR96624 When assigning a reshaped constant array of shape [2,0] to a variable fails with an invalid memory access.  If a varibale with the parameter attribute is initialised with the same reshape there is no runtime error. 2020-08-20  Steven G. Kargl  gcc/fortran/     PR fortran/96624     * simplify.c (gfc_simplifiy_reshape): Add new variable "zerosize".     Set zerosize if any of the result shape ranks are zero.  After     setting the result shapes, if zerosize is set jump to new label     "sizezero".  Add label "sizezero" just before clearing index and     returning result. 2020-08-20  Mark Eggleston gcc/testsuite/     PR fortran/96624     *gfortran/pr96624.f90: New test. -- https://www.codethink.co.uk/privacy.html --------------83E23468DA5C5AC70E9CBD35 Content-Type: text/x-patch; charset=UTF-8; name="0001-Fortran-Runtime-error-reshape-constant-array-assignm.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0001-Fortran-Runtime-error-reshape-constant-array-assignm.pa"; filename*1="tch" >From ab94bb744a7d64751f6b93cc56ad3ed5fe5cfc81 Mon Sep 17 00:00:00 2001 From: Mark Eggleston Date: Mon, 17 Aug 2020 13:50:28 +0100 Subject: [PATCH] Fortran : Runtime error, reshape constant array assignment PR96624 When assigning a reshaped constant array of shape [2,0] to a variable fails with an invalid memory access. If a varibale with the parameter attribute is initialised with the same reshape there is no runtime error. 2020-08-20 Steven G. Kargl gcc/fortran/ PR fortran/96624 * simplify.c (gfc_simplifiy_reshape): Add new variable "zerosize". Set zerosize if any of the result shape ranks are zero. After setting the result shapes, if zerosize is set jump to new label "sizezero". Add label "sizezero" just before clearing index and returning result. 2020-08-20 Mark Eggleston gcc/testsuite/ PR fortran/96624 *gfortran/pr96624.f90: New test. --- gcc/fortran/simplify.c | 11 ++++++++++- gcc/testsuite/gfortran.dg/pr96624.f90 | 10 ++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gfortran.dg/pr96624.f90 diff --git a/gcc/fortran/simplify.c b/gcc/fortran/simplify.c index eb8b2afeb29..0d77d289651 100644 --- a/gcc/fortran/simplify.c +++ b/gcc/fortran/simplify.c @@ -6721,6 +6721,7 @@ gfc_simplify_reshape (gfc_expr *source, gfc_expr *shape_exp, unsigned long j; size_t nsource; gfc_expr *e, *result; + bool zerosize = false; /* Check that argument expression types are OK. */ if (!is_constant_array_expr (source) @@ -6843,7 +6844,14 @@ gfc_simplify_reshape (gfc_expr *source, gfc_expr *shape_exp, result->rank = rank; result->shape = gfc_get_shape (rank); for (i = 0; i < rank; i++) - mpz_init_set_ui (result->shape[i], shape[i]); + { + mpz_init_set_ui (result->shape[i], shape[i]); + if (shape[i] == 0) + zerosize = true; + } + + if (zerosize) + goto sizezero; while (nsource > 0 || npad > 0) { @@ -6893,6 +6901,7 @@ inc: break; } +sizezero: mpz_clear (index); return result; diff --git a/gcc/testsuite/gfortran.dg/pr96624.f90 b/gcc/testsuite/gfortran.dg/pr96624.f90 new file mode 100644 index 00000000000..a4cfe5c3279 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/pr96624.f90 @@ -0,0 +1,10 @@ +! { dg-do run } + +program test + integer :: a(2,0) + character(4) :: buffer + a = reshape([1,2,3,4], [2,0]) + write(buffer,"(2a1)") ">", "<" + if (trim(buffer).ne."><") stop 1 +end + -- 2.11.0 --------------83E23468DA5C5AC70E9CBD35--