From patchwork Sat Jan 10 14:59:03 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andre Vehreschild X-Patchwork-Id: 427369 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 0A0A814018C for ; Sun, 11 Jan 2015 01:59:26 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=SsjisnAcEqGAUS8ZZ3ylBJg+6ytr+cNmad2arngC+XEg36S2jzw+s XkWIb+hIzVQNlP17kyZqobCCxoVYvAuT3ENnvEgmJ93lPK5msWvlo1qFOTiyMqAa S0OUteMh9xvOkRBTViIapK2+3nj3zXCsTpGZghlDhYPkdcS1aSLOPA= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=6DDyEKZRt8IPlfssDfe8yMtCIfI=; b=RKF1NVM4ruMcHsKfWIFX 41jjRhkWiofvbCgzuN7xsLnzUdEaLxvXcnvwJB5xraAHw/lxAOxdI0UnGQdP1j6g 6DhnDc3D6dBBY3ORDpQKoXjvsvvmTk1GJE5G8c23gnerxxxgKIzSg7bRTXoaZ9ux kwXJ9JeM7q/yF/DTQq1Bn/s= Received: (qmail 21238 invoked by alias); 10 Jan 2015 14:59:12 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 21220 invoked by uid 89); 10 Jan 2015 14:59:10 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.3 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 X-Spam-User: qpsmtpd, 2 recipients X-HELO: mout.gmx.net Received: from mout.gmx.net (HELO mout.gmx.net) (212.227.17.20) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Sat, 10 Jan 2015 14:59:08 +0000 Received: from vepi2 ([84.63.49.248]) by mail.gmx.com (mrgmx101) with ESMTPSA (Nemesis) id 0MdWO8-1YQUqh3GkG-00PQjA; Sat, 10 Jan 2015 15:59:04 +0100 Date: Sat, 10 Jan 2015 15:59:03 +0100 From: Andre Vehreschild To: GCC-Fortran-ML , GCC-Patches-ML Subject: [Fortran, Patch] PR60334 - Segmentation fault on character pointer assignments Message-ID: <20150110155903.622217bc@vepi2> MIME-Version: 1.0 X-UI-Out-Filterresults: notjunk:1; Hi all, attached patch fixes the bug reported in pr 60334. The issue here was that the function's result being (a pointer to) a deferred length char array. The string length for the result value was wrapped in a local variable, whose value was never written back to the string length of the result. This lead the calling routine to take the length of the result to be random leading to a crash. This patch addresses the issue by preventing the instantiation of the local var and instead using a reference to the parameter. This not only saves one value on the stack, but also because for small functions the compiler will hold all parameters in registers for a significant level of optimization, all the overhead of memory access (I hope :-). Bootstraps and regtests ok on x86_64-linux-gnu. - Andre diff --git a/gcc/fortran/trans-decl.c b/gcc/fortran/trans-decl.c index 1e74125..86873f7 100644 --- a/gcc/fortran/trans-decl.c +++ b/gcc/fortran/trans-decl.c @@ -1333,12 +1333,30 @@ gfc_get_symbol_decl (gfc_symbol * sym) (sym->ts.u.cl->passed_length == sym->ts.u.cl->backend_decl)) sym->ts.u.cl->backend_decl = NULL_TREE; - if (sym->ts.deferred && fun_or_res - && sym->ts.u.cl->passed_length == NULL - && sym->ts.u.cl->backend_decl) + if (sym->ts.deferred && byref) { - sym->ts.u.cl->passed_length = sym->ts.u.cl->backend_decl; - sym->ts.u.cl->backend_decl = NULL_TREE; + /* The string length of a deferred char array is stored in the + parameter at sym->ts.u.cl->backend_decl as a reference and + marked as a result. Exempt this variable from generating a + temporary for it. */ + if (sym->attr.result) + { + /* We need to insert a indirect ref for param decls. */ + if (sym->ts.u.cl->backend_decl + && TREE_CODE (sym->ts.u.cl->backend_decl) == PARM_DECL) + sym->ts.u.cl->backend_decl = + build_fold_indirect_ref (sym->ts.u.cl->backend_decl); + } + /* For all other parameters make sure, that they are copied so + that the value and any modifications are local to the routine + by generating a temporary variable. */ + else if (sym->attr.function + && sym->ts.u.cl->passed_length == NULL + && sym->ts.u.cl->backend_decl) + { + sym->ts.u.cl->passed_length = sym->ts.u.cl->backend_decl; + sym->ts.u.cl->backend_decl = NULL_TREE; + } } if (sym->ts.u.cl->backend_decl == NULL_TREE) diff --git a/gcc/testsuite/gfortran.dg/deferred_type_param_6.f90 b/gcc/testsuite/gfortran.dg/deferred_type_param_6.f90 index eb00778..7016070 100644 --- a/gcc/testsuite/gfortran.dg/deferred_type_param_6.f90 +++ b/gcc/testsuite/gfortran.dg/deferred_type_param_6.f90 @@ -2,15 +2,20 @@ ! ! PR fortran/51055 ! PR fortran/49110 -! +! PR fortran/60334 subroutine test() implicit none integer :: i = 5 character(len=:), allocatable :: s1 + character(len=:), pointer :: s2 + character(len=5), target :: fifeC = 'FIVEC' call sub(s1, i) if (len(s1) /= 5) call abort() if (s1 /= "ZZZZZ") call abort() + s2 => subfunc() + if (len(s2) /= 5) call abort() + if (s2 /= "FIVEC") call abort() contains subroutine sub(str,j) character(len=:), allocatable :: str @@ -19,6 +24,12 @@ contains if (len(str) /= 5) call abort() if (str /= "ZZZZZ") call abort() end subroutine sub + function subfunc() result(res) + character(len=:), pointer :: res + res => fifec + if (len(res) /= 5) call abort() + if (res /= "FIVEC") call abort() + end function subfunc end subroutine test program a