From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 66616 invoked by alias); 12 Oct 2015 05:18:54 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 66595 invoked by uid 89); 12 Oct 2015 05:18:53 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.9 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_PASS,T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-Spam-User: qpsmtpd, 2 recipients X-HELO: sender153-mail.zoho.com Received: from sender153-mail.zoho.com (HELO sender153-mail.zoho.com) (74.201.84.153) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Mon, 12 Oct 2015 05:18:52 +0000 Received: from mail.zoho.com by mx.zohomail.com with SMTP id 1444627128285841.6257716214683; Sun, 11 Oct 2015 22:18:48 -0700 (PDT) Received: from [209.169.24.247] by mail.zoho.com with HTTP;Sun, 11 Oct 2015 22:18:48 -0700 (PDT) Date: Mon, 12 Oct 2015 05:18:00 -0000 From: Louis Krupp To: "gcc-patches" , "fortran" Message-ID: <1505a7c9fab.f52df49364480.2316667444406332595@zoho.com> Subject: Possible patch for PR fortran/67806 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_Part_43654_861662740.1444627128231" User-Agent: Zoho Mail X-Zoho-Virus-Status: 1 X-SW-Source: 2015-10/txt/msg01091.txt.bz2 ------=_Part_43654_861662740.1444627128231 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Content-length: 2094 The problem involves a derived type with a character component declared CHA= RACTER(NULL()) or CHARACTER(NULL(n)), where mold argument n is an integer p= ointer. I might be missing something, but I'm not sure there's a point to having a = character variable whose length is the target of a null pointer. This prog= ram, for example, crashes with a SEGV reported at line 10 (with line 11 deleted, the program = runs to completion): 1 program z 2 implicit none 3 integer, target :: k =3D 0 4 integer, pointer :: p =3D> k 5 nullify(p) 6 call s(p) 7 contains 8 subroutine s(n) 9 integer, pointer :: n 10 character (len=3Dn) q 11 q =3D 'a' 12 end subroutine 13 end program What to do with CHARACTER(NULL([mold])), besides fix the ICE? It might hav= e been possible to generate code to define a null pointer and generate code= to dereference it and get the expected SEGV, but it seemed easier and poss= ibly more productive to treat CHARACTER(NULL(..)) as an error. I don't kno= w how what the standard has to say about this. It might have been one of t= hose things its authors never thought about. Since this problem is detected at different places in the code, the attache= d test case gives the following errors with the attached patch: ! { dg-do compile } ! PR 67806 ! 1. Initialize a variable of derived type with a string component having ! a length that is the target of the NULL intrinsic. ! 2. Declare a derived type with a string component having a length that is ! the target of the NULL intrinsic with an integer mold argument. subroutine s1 type t character(null()) :: c ! { dg-error "is target of NULL pointer" } 1 Error: Character length of component =E2=80=98c=E2=80=99 is target of NULL = pointer at (1) end type type(t) :: x =3D t('a') end subroutine subroutine s2 integer, pointer :: n type t character(null(n)) :: c ! { dg-error "is target of NULL pointer" } 1 Error: Character length is target of NULL pointer at (1) end type end subroutine ------=_Part_43654_861662740.1444627128231 Content-Type: application/octet-stream; name=init_bad_string_comp_1.f90 Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename=init_bad_string_comp_1.f90 Content-length: 604 ! { dg-do compile } ! PR 67806 ! 1. Initialize a variable of derived type with a string component having ! a length that is the target of the NULL intrinsic. ! 2. Declare a derived type with a string component having a length that is ! the target of the NULL intrinsic with an integer mold argument. subroutine s1 type t character(null()) :: c ! { dg-error "is target of NULL pointer" } end type type(t) :: x = t('a') end subroutine subroutine s2 integer, pointer :: n type t character(null(n)) :: c ! { dg-error "is target of NULL pointer" } end type end subroutine ------=_Part_43654_861662740.1444627128231 Content-Type: text/plain; charset=us-ascii; name=patch.txt Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename=patch.txt Content-length: 1699 Index: gcc/fortran/resolve.c =================================================================== --- gcc/fortran/resolve.c (revision 228700) +++ gcc/fortran/resolve.c (working copy) @@ -1134,7 +1134,8 @@ resolve_structure_cons (gfc_expr *expr, int init) t = true; if (expr->ts.type == BT_DERIVED) - resolve_fl_derived0 (expr->ts.u.derived); + if (!resolve_fl_derived0 (expr->ts.u.derived)) + return false; cons = gfc_constructor_first (expr->value.constructor); @@ -10882,6 +10883,13 @@ resolve_charlen (gfc_charlen *cl) } } + if (cl->length && cl->length->expr_type == EXPR_NULL) + { + gfc_error ("Character length is target of NULL pointer at %L", + &cl->length->where); + return false; + } + /* "If the character length parameter value evaluates to a negative value, the length of character entities declared is zero." */ if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0) @@ -13090,10 +13098,16 @@ resolve_fl_derived0 (gfc_symbol *sym) || (!resolve_charlen(c->ts.u.cl)) || !gfc_is_constant_expr (c->ts.u.cl->length)) { - gfc_error ("Character length of component %qs needs to " - "be a constant specification expression at %L", + gfc_error (c->ts.u.cl->length && + c->ts.u.cl->length->expr_type == EXPR_NULL ? + "Character length of component %qs is target of " + "NULL pointer at %L" + : + "Character length of component %qs needs to " + "be a constant specification expression at %L", c->name, - c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc); + c->ts.u.cl->length ? + &c->ts.u.cl->length->where : &c->loc); return false; } } ------=_Part_43654_861662740.1444627128231--