From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mout.gmx.net (mout.gmx.net [212.227.17.22]) by sourceware.org (Postfix) with ESMTPS id 9026D388A035; Mon, 26 Jul 2021 21:55:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9026D388A035 X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [93.207.81.171] ([93.207.81.171]) by web-mail.gmx.net (3c-app-gmx-bap37.server.lan [172.19.172.107]) (via HTTP); Mon, 26 Jul 2021 23:55:11 +0200 MIME-Version: 1.0 Message-ID: From: Harald Anlauf To: Tobias Burnus Cc: fortran , gcc-patches Subject: Re: [PATCH] PR fortrsn/101564 - ICE in resolve_allocate_deallocate, at fortran/resolve.c:8169 Content-Type: multipart/mixed; boundary=nika-93a543b9-c9cf-4193-a18e-7c500b195bbf Date: Mon, 26 Jul 2021 23:55:11 +0200 Importance: normal Sensitivity: Normal In-Reply-To: <217aa918-f12a-ebb5-2941-63b87c84b69c@codesourcery.com> References: <9b6187f0-d3dd-bb2d-d6f3-ada831cdecf0@codesourcery.com> <217aa918-f12a-ebb5-2941-63b87c84b69c@codesourcery.com> X-UI-Message-Type: mail X-Priority: 3 X-Provags-ID: V03:K1:1IlYUMP9MTdWyE4yf/+xX+Z5sBHpHloyz3eMEwppqi+Rs4Ovf3TD4m5kYG+MhDNwmCBSv vYVApsxF0kb0hmLyVMq2+ZQOPSPpmrP1+PjGqCPlsLApiOMv2n6r4wWECUw/geHl929BxkAAFtbX IZgIQaV12MrOEpB9fsP2hlt9cbwa5IJg8DtGqFDCXGAgD7u+OuridXqE7MinkzukRKTNW9gjHpKj T0NT8SrewAlKfUf5VVnB5/S5Aa0wyxmC6IET1bh0RGnLVwziYbJNn5mxHV1QuNMuxArciVUP5Uwz 1o= X-UI-Out-Filterresults: notjunk:1;V03:K0:7U0uUVYuDEc=:vPLG6p5GGT8fLXEvA/E7a4 rmdvHyxmqW5B2FVRFZsSQY/a5LoA5Ae2AvufEticGMC1RQ1QiB6uSwt2R+dZP2h0YAYHVUe2R SAtJp1GjDhht9eVnKtw7mFEXNhFbQLtiIcuCRI6Cba/BZZHLmCfOAuFQpnqTrqmUNkhrnKgx5 EV88Yc7PRttIlIdgbLvKCCiyCqVm2+lKrDT2EZBzbtI8nFY07Yd/CmpsergyhpwBAw50JvLfk KGsyRINY8lkToBGyQydZU8BU+xtJQzuDiATh7QFIyFN8vQS+kcC7FZPqMzBwig/ktD3mIzgWg XMyj/KhJ4206+fXO/ysDIU8gGQZDZ2NMuOqDF581obydpbVRCiJaIlHxDD7gzPSReqR/LkZeS gM7HGCtpz/wVF7Bc87HJjtO9k/V3rfr7tokmzvenmnlkdiRGDLIOopDcOLpvLlX90mdWXCP9P D9WwMd0yZiXEMmNWky+7PgF+tq6uV5E4vuB4K5UXkaw3ToX06jQxM0scUQSHi3htA890ZIKyF EusFT1QQQVinIDFkbZUpSInpmfYva5MhvChvv7tctJIKKDg2+ZxuzvelpgrGD0nO4v+geh1kv iq+iiOaTGgCHr+x3zk/wOi9wwQMYaFB+gWfnVrBMXE9gdlXnvksu07L3yuH+QoKi/rKZDREq0 5C5nkt3GIex4990EiBqusjGAesUPW3i27odskFAyambBu9sHcGusSyCkvevB2QPO8r+c07dW0 SZzLASpKqIOEcglcHR6zxrFO1ezQmmYKL9vTUId7h1qkMKWSENiCfwEXktqp/Nu6480eguIjz iajJFTtXPUUyDxb0+JpCVFg4sMbtg== X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, FREEMAIL_FROM, GIT_PATCH_0, KAM_NUMSUBJECT, KAM_SHORT, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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-List-Received-Date: Mon, 26 Jul 2021 21:55:20 -0000 --nika-93a543b9-c9cf-4193-a18e-7c500b195bbf Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hi Tobias, > > This works as expected with Intel and AOCC, but gives a > > syntax error with every gfortran tested because of match=2Ec: > > > > alloc_opt_list: > > m =3D gfc_match (" stat =3D %v", &tmp); >=20 > I think we can simply change that one to %e; the definable > check should ensure that any non variable (in the Fortran sense) > is rejected=2E >=20 > And we should update the comment for %v / match_variable to state > that it does not include function references=2E I've updated this for ALLOCATE/DEALLOCATE and STAT/ERRMSG, see attached patch=2E This required updating the error messages of two existing files in the testsuite=2E > Also affected: Some I/O items, a bunch of other stat=3D%v and > errmsg=3D%v=2E We should rather open a separate PR on auditing the related uses of gfc_match=2E > Talking about errmsg: In the same function, the same check is > done for errmsg as for stat =E2=80=93 hence, the patch should update > also errmsg=2E Done=2E > >> Additionally, I have to admit that I do not understand the > >> following existing condition, which you did not touch: > >> > >> if ((stat->ts=2Etype !=3D BT_INTEGER > >> && !(stat->ref && (stat->ref->type =3D=3D REF_ARRAY > >> || stat->ref->type =3D=3D REF_COMPONE= NT))) > >> || stat->rank > 0) > >> gfc_error ("Stat-variable at %L must be a scalar INTEGER " > >> "variable", &stat->where); > >> > >> I mean the ts=2Etype !=3D BT_INTEGER and stat->rank !=3D 0 is clear, > >> but what's the reason for the refs? > > Well, that needs to be answered by Steve (see commit 3759634)=2E >=20 > (https://gcc=2Egnu=2Eorg/g:3759634f3208cbc1226bec19d22cbff989a287c3 (svn > r145331)) >=20 > The reason for the ref checks is unclear and seem to be wrong=2E The add= ed > testcases also only use 'x' (real) and n or i (integer) as input, i=2Ee= =2E > they do not exercise this=2E I did not look for the patch email for reas= oning=2E Well, there is some text in the standard that I added in front of the for loops, and this code is now exercised in the new testcase=2E Regtested on x86_64-pc-linux-gnu=2E OK? Thanks, Harald Fortran: ICE in resolve_allocate_deallocate for invalid STAT argument gcc/fortran/ChangeLog: PR fortran/101564 * match=2Ec (gfc_match): Fix comment for %v code=2E (gfc_match_allocate, gfc_match_deallocate): Replace use of %v code by %e in gfc_match to allow for function references as STAT and ERRMSG arguments=2E * resolve=2Ec (resolve_allocate_deallocate): Avoid NULL pointer dereferences and shortcut for bad STAT and ERRMSG argument to (DE)ALLOCATE=2E gcc/testsuite/ChangeLog: PR fortran/101564 * gfortran=2Edg/allocate_stat_3=2Ef90: New test=2E * gfortran=2Edg/allocate_stat=2Ef90: Adjust error messages=2E * gfortran=2Edg/implicit_11=2Ef90: Adjust error messages=2E --nika-93a543b9-c9cf-4193-a18e-7c500b195bbf Content-Type: text/x-patch Content-Disposition: attachment; filename=pr101564-v2.patch Content-Transfer-Encoding: quoted-printable diff --git a/gcc/fortran/match.c b/gcc/fortran/match.c index d148de3e3b5..b1105481099 100644 =2D-- a/gcc/fortran/match.c +++ b/gcc/fortran/match.c @@ -1109,7 +1109,8 @@ gfc_match_char (char c) %t Matches end of statement. %o Matches an intrinsic operator, returned as an INTRINSIC enum. %l Matches a statement label - %v Matches a variable expression (an lvalue) + %v Matches a variable expression (an lvalue, except function referenc= es + having a data pointer result) % Matches a required space (in free form) and optional spaces. */ match @@ -4405,7 +4406,7 @@ gfc_match_allocate (void) alloc_opt_list: - m =3D gfc_match (" stat =3D %v", &tmp); + m =3D gfc_match (" stat =3D %e", &tmp); if (m =3D=3D MATCH_ERROR) goto cleanup; if (m =3D=3D MATCH_YES) @@ -4434,7 +4435,7 @@ alloc_opt_list: goto alloc_opt_list; } - m =3D gfc_match (" errmsg =3D %v", &tmp); + m =3D gfc_match (" errmsg =3D %e", &tmp); if (m =3D=3D MATCH_ERROR) goto cleanup; if (m =3D=3D MATCH_YES) @@ -4777,7 +4778,7 @@ gfc_match_deallocate (void) dealloc_opt_list: - m =3D gfc_match (" stat =3D %v", &tmp); + m =3D gfc_match (" stat =3D %e", &tmp); if (m =3D=3D MATCH_ERROR) goto cleanup; if (m =3D=3D MATCH_YES) @@ -4799,7 +4800,7 @@ dealloc_opt_list: goto dealloc_opt_list; } - m =3D gfc_match (" errmsg =3D %v", &tmp); + m =3D gfc_match (" errmsg =3D %e", &tmp); if (m =3D=3D MATCH_ERROR) goto cleanup; if (m =3D=3D MATCH_YES) diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c index 45c3ad387ac..809a4ad86d1 100644 =2D-- a/gcc/fortran/resolve.c +++ b/gcc/fortran/resolve.c @@ -8165,6 +8165,12 @@ resolve_allocate_deallocate (gfc_code *code, const = char *fcn) gfc_error ("Stat-variable at %L must be a scalar INTEGER " "variable", &stat->where); + if (stat->expr_type =3D=3D EXPR_CONSTANT || stat->symtree =3D=3D NU= LL) + goto done_stat; + + /* F2018:9.7.4: The stat-variable shall not be allocated or dealloc= ated + * within the ALLOCATE or DEALLOCATE statement in which it appears = ... + */ for (p =3D code->ext.alloc.list; p; p =3D p->next) if (p->expr->symtree->n.sym->name =3D=3D stat->symtree->n.sym->name) { @@ -8192,6 +8198,8 @@ resolve_allocate_deallocate (gfc_code *code, const c= har *fcn) } } +done_stat: + /* Check the errmsg variable. */ if (errmsg) { @@ -8215,6 +8223,12 @@ resolve_allocate_deallocate (gfc_code *code, const = char *fcn) gfc_error ("ERRMSG variable at %L shall be a scalar default CHARACTER " "variable", &errmsg->where); + if (errmsg->expr_type =3D=3D EXPR_CONSTANT || errmsg->symtree =3D= =3D NULL) + goto done_errmsg; + + /* F2018:9.7.5: The errmsg-variable shall not be allocated or deall= ocated + * within the ALLOCATE or DEALLOCATE statement in which it appears = ... + */ for (p =3D code->ext.alloc.list; p; p =3D p->next) if (p->expr->symtree->n.sym->name =3D=3D errmsg->symtree->n.sym->name) { @@ -8242,6 +8256,8 @@ resolve_allocate_deallocate (gfc_code *code, const c= har *fcn) } } +done_errmsg: + /* Check that an allocate-object appears only once in the statement. *= / for (p =3D code->ext.alloc.list; p; p =3D p->next) diff --git a/gcc/testsuite/gfortran.dg/allocate_stat.f90 b/gcc/testsuite/g= fortran.dg/allocate_stat.f90 index 7f9eaf58d6d..f8a12913c91 100644 =2D-- a/gcc/testsuite/gfortran.dg/allocate_stat.f90 +++ b/gcc/testsuite/gfortran.dg/allocate_stat.f90 @@ -38,7 +38,7 @@ function func2() result(res) implicit none real, pointer :: gain integer :: res - allocate (gain,STAT=3Dfunc2) ! { dg-error "is not a variable" } + allocate (gain,STAT=3Dfunc2) ! { dg-error "requires an argument list" } deallocate(gain) res =3D 0 end function func2 @@ -51,7 +51,7 @@ subroutine sub() end interface real, pointer :: gain integer, parameter :: res =3D 2 - allocate (gain,STAT=3Dfunc2) ! { dg-error "is not a variable" } + allocate (gain,STAT=3Dfunc2) ! { dg-error "requires an argument list" } deallocate(gain) end subroutine sub @@ -68,9 +68,9 @@ contains end function one subroutine sub() integer, pointer :: p - allocate(p, stat=3Done) ! { dg-error "is not a variable" } + allocate(p, stat=3Done) ! { dg-error "requires an argument list" } if(associated(p)) deallocate(p) - allocate(p, stat=3Dtwo) ! { dg-error "is not a variable" } + allocate(p, stat=3Dtwo) ! { dg-error "requires an argument list" } if(associated(p)) deallocate(p) end subroutine sub end module test diff --git a/gcc/testsuite/gfortran.dg/allocate_stat_3.f90 b/gcc/testsuite= /gfortran.dg/allocate_stat_3.f90 new file mode 100644 index 00000000000..c5ba6b892da =2D-- /dev/null +++ b/gcc/testsuite/gfortran.dg/allocate_stat_3.f90 @@ -0,0 +1,50 @@ +! { dg-do compile } +! PR fortran/101564 - ICE in resolve_allocate_deallocate + +program p + integer, allocatable :: x(:) + integer :: stat + integer, pointer :: A + integer, target :: ptr + real, target :: r + character(4) :: c + type t + integer :: stat + real :: r + end type t + type(t), allocatable :: y + allocate (x(2), stat=3Dstat) + deallocate (x, stat=3Dstat) + allocate (A, stat=3Df()) + deallocate (A, stat=3Df()) + allocate (A, stat=3Dy%stat) + deallocate (A, stat=3Dy%stat) + allocate (A, stat=3Dy%r) + deallocate (A, stat=3Dy%r) + allocate (x(2), stat=3Dstat%kind) ! { dg-error "STAT tag" } + deallocate (x, stat=3Dstat%kind) ! { dg-error "STAT variable" } + allocate (A, stat=3DA%kind) ! { dg-error "STAT tag" } + deallocate (A, stat=3DA%kind) ! { dg-error "STAT variable" } + allocate (A, stat=3Dc%len) ! { dg-error "STAT tag" } + deallocate (A, stat=3Dc%len) ! { dg-error "STAT variable" } + allocate (y, stat=3Dy%stat) ! { dg-error "within the same ALLOCATE stat= ement" } + allocate (y, stat=3Dy%r) ! { dg-error "within the same ALLOCATE stat= ement" } + allocate (y, stat=3Dr) ! { dg-error "must be a scalar INTEGER vari= able" } + allocate (y, stat=3Dg()) ! { dg-error "must be a scalar INTEGER vari= able" } + deallocate (y, stat=3Dg()) ! { dg-error "must be a scalar INTEGER vari= able" } + allocate (A, stat=3Df) ! { dg-error "requires an argument list" } + deallocate (A, stat=3Df) ! { dg-error "requires an argument list" } + allocate (y, stat=3Dg) ! { dg-error "requires an argument list" } + deallocate (y, stat=3Dg) ! { dg-error "requires an argument list" } + allocate (A, stat=3Df(), errmsg=3D"") ! { dg-error "ERRMSG variable" = } + deallocate (A, stat=3Df(), errmsg=3D"") ! { dg-error "ERRMSG variable" = } +contains + integer function f() + pointer :: f + f =3D ptr + end function f + real function g() + pointer :: g + g =3D r + end function g +end diff --git a/gcc/testsuite/gfortran.dg/implicit_11.f90 b/gcc/testsuite/gfo= rtran.dg/implicit_11.f90 index 61091ec41a0..8f93704ac4c 100644 =2D-- a/gcc/testsuite/gfortran.dg/implicit_11.f90 +++ b/gcc/testsuite/gfortran.dg/implicit_11.f90 @@ -31,6 +31,6 @@ SUBROUTINE AD0001 REAL RLA1(:) ALLOCATABLE RLA1 - ALLOCATE (RLA1(NF10), STAT =3D ISTAT2) ! { dg-error "is not a vari= able" } + ALLOCATE (RLA1(NF10), STAT =3D ISTAT2) ! { dg-error "requires an a= rgument list" } END SUBROUTINE END MODULE tests2 --nika-93a543b9-c9cf-4193-a18e-7c500b195bbf--