From patchwork Thu Dec 1 23:23:40 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steve Kargl X-Patchwork-Id: 86171 Delivered-To: patch@linaro.org Received: by 10.140.20.101 with SMTP id 92csp2192qgi; Thu, 1 Dec 2016 15:24:08 -0800 (PST) X-Received: by 10.99.50.67 with SMTP id y64mr73712525pgy.146.1480634648581; Thu, 01 Dec 2016 15:24:08 -0800 (PST) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id h8si2056494pli.261.2016.12.01.15.24.08 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 01 Dec 2016 15:24:08 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-return-443277-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org; spf=pass (google.com: domain of gcc-patches-return-443277-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-443277-patch=linaro.org@gcc.gnu.org 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:cc:subject:message-id:reply-to:references:mime-version :content-type:in-reply-to; q=dns; s=default; b=iX0fPK6HIQmO8xhAL d8E9hcOyGY1n90+9h+wkrhKQlaY2pSD5BNlpHyYhQpMy5A44xq4DQzMskfLn4ww3 0pUqGtMz/fUBbK3iXd3swEkFphchnDePZ/xevxXGvMffM1KlwmkRQIZLCF7GxHaS hkUHRuJb6TC7U6VdprRaAi6nlA= 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:cc:subject:message-id:reply-to:references:mime-version :content-type:in-reply-to; s=default; bh=s6/N02j8X3b/ewU25EAUOKf kCtY=; b=ibmAxxxK+fRA2zoAYV1tGIkXB/id7i5zY7ivEgRj4I3vyg7uCdHjyHI GhYQiXg+fLq0jwAC4eNJvluPZ44qnq33X+EXkCOxU3bgfI/HuSn1tHYbg6ziyq73 9AFn7xjJOzJhqgcqBuHK3cX4gllry2d38ii+K3omXfWWfhyI/WTs= Received: (qmail 112568 invoked by alias); 1 Dec 2016 23:23:55 -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 112461 invoked by uid 89); 1 Dec 2016 23:23:53 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.9 required=5.0 tests=AWL, BAYES_00, KAM_ASCII_DIVIDERS, KAM_LAZY_DOMAIN_SECURITY, RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy=recommendations, resending, UD:cl, agent X-Spam-User: qpsmtpd, 2 recipients X-HELO: troutmask.apl.washington.edu Received: from troutmask.apl.washington.edu (HELO troutmask.apl.washington.edu) (128.95.76.21) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 01 Dec 2016 23:23:43 +0000 Received: from troutmask.apl.washington.edu (localhost [127.0.0.1]) by troutmask.apl.washington.edu (8.15.2/8.15.2) with ESMTPS id uB1NNebT061530 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Thu, 1 Dec 2016 15:23:40 -0800 (PST) (envelope-from sgk@troutmask.apl.washington.edu) Received: (from sgk@localhost) by troutmask.apl.washington.edu (8.15.2/8.15.2/Submit) id uB1NNej2061529; Thu, 1 Dec 2016 15:23:40 -0800 (PST) (envelope-from sgk) Date: Thu, 1 Dec 2016 15:23:40 -0800 From: Steve Kargl To: "Punnoose, Elizebeth" Cc: "fortran@gcc.gnu.org" , "gcc-patches@gcc.gnu.org" Subject: Re: [PATCH] PR fortran/77505 -- Treat negative character length as LEN=0 Message-ID: <20161201232340.GA61471@troutmask.apl.washington.edu> Reply-To: kargl@uw.edu References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.6.1 (2016-04-27) On Wed, Nov 30, 2016 at 05:13:28AM +0000, Punnoose, Elizebeth wrote: > Please excuse the messy formatting in my initial mail. Resending > with proper formatting. > > This patch checks for negative character length in the array > constructor, and treats it as LEN=0. > > A warning message is also printed if bounds checking is enabled. > > Bootstrapped and regression tested the patch on x86_64-linux-gnu > and aarch64-linux-gnu. > Thanks. After regression testing on x86_64-*-freebsd, I committed the attached patch. Not sure if the whitespace got messed up by my email agent, but I needed to reformat your testcases. I took the opportunity to rename and improve the testcases. The improvements check that in fact len=0 and that a warning is issued. Hopefully, you're inclined to submit additional patches in the future. A few recommendations are to include the text of your ChangeLog entry in body of the email, for example, 2016-12-01 Elizebeth Punnoose PR fortran/77505 * trans-array.c (trans_array_constructor): Treat negative character length as LEN = 0. 2016-12-01 Elizebeth Punnoose PR fortran/77505 * gfortran.dg/char_length_20.f90: New test. * gfortran.dg/char_length_21.f90: Ditto. (Note, 2 spaces before and after your name.) Then attach the patch to the email. This hopefully will prevent formatting issues with various email clients/servers. -- Steve Index: gcc/fortran/trans-array.c =================================================================== --- gcc/fortran/trans-array.c (revision 243134) +++ gcc/fortran/trans-array.c (working copy) @@ -2226,6 +2226,8 @@ trans_array_constructor (gfc_ss * ss, lo gfc_ss_info *ss_info; gfc_expr *expr; gfc_ss *s; + tree neg_len; + char *msg; /* Save the old values for nested checking. */ old_first_len = first_len; @@ -2271,6 +2273,29 @@ trans_array_constructor (gfc_ss * ss, lo gfc_conv_expr_type (&length_se, expr->ts.u.cl->length, gfc_charlen_type_node); ss_info->string_length = length_se.expr; + + /* Check if the character length is negative. If it is, then + set LEN = 0. */ + neg_len = fold_build2_loc (input_location, LT_EXPR, + boolean_type_node, ss_info->string_length, + build_int_cst (gfc_charlen_type_node, 0)); + /* Print a warning if bounds checking is enabled. */ + if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) + { + msg = xasprintf ("Negative character length treated as LEN = 0"); + gfc_trans_runtime_check (false, true, neg_len, &length_se.pre, + where, msg); + free (msg); + } + + ss_info->string_length + = fold_build3_loc (input_location, COND_EXPR, + gfc_charlen_type_node, neg_len, + build_int_cst (gfc_charlen_type_node, 0), + ss_info->string_length); + ss_info->string_length = gfc_evaluate_now (ss_info->string_length, + &length_se.pre); + gfc_add_block_to_block (&outer_loop->pre, &length_se.pre); gfc_add_block_to_block (&outer_loop->post, &length_se.post); } Index: gcc/testsuite/gfortran.dg/char_length_20.f90 =================================================================== --- gcc/testsuite/gfortran.dg/char_length_20.f90 (nonexistent) +++ gcc/testsuite/gfortran.dg/char_length_20.f90 (working copy) @@ -0,0 +1,13 @@ +! { dg-do run } +! { dg-options "-fcheck=bounds" } +program rabbithole + implicit none + character(len=:), allocatable :: text_block(:) + integer i, ii + character(len=10) :: cten='abcdefghij' + character(len=20) :: ctwenty='abcdefghijabcdefghij' + ii = -6 + text_block=[ character(len=ii) :: cten, ctwenty ] + if (any(len_trim(text_block) /= 0)) call abort +end program rabbithole +! { dg-output "At line 10 of file .*char_length_20.f90.*Fortran runtime warning: Negative character length treated as LEN = 0" } Index: gcc/testsuite/gfortran.dg/char_length_21.f90 =================================================================== --- gcc/testsuite/gfortran.dg/char_length_21.f90 (nonexistent) +++ gcc/testsuite/gfortran.dg/char_length_21.f90 (working copy) @@ -0,0 +1,11 @@ +! { dg-do run } +program rabbithole + implicit none + character(len=:), allocatable :: text_block(:) + integer i, ii + character(len=10) :: cten='abcdefghij' + character(len=20) :: ctwenty='abcdefghijabcdefghij' + ii = -6 + text_block = [character(len=ii) :: cten, ctwenty] + if (any(len_trim(text_block) /= 0)) call abort +end program rabbithole