From patchwork Thu Sep 9 19:38:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harald Anlauf X-Patchwork-Id: 1526276 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=SYjgn332; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4H58St41dMz9sXS for ; Fri, 10 Sep 2021 05:38:52 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 03F62384B0F0 for ; Thu, 9 Sep 2021 19:38:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 03F62384B0F0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1631216330; bh=LTpF23mPAcOryWs+p5IgruyhwFB1lHZPJAYtos3HGzM=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=SYjgn332FIHfHezgEn1wQX2D0rd7QI2NGDUbbR4mffsODLxw8QS9WxtZoJPtYikM1 wYR6eeFnh1Tld0Ol5yZj7g3LHZlH2RdECGeo0nfIw/8L/L519EPO5TRKxsLcQ/8PXj wMSniQT6vo8rp2uVrRCDxjbfjWrjae96oCA1JsFY= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout.gmx.net (mout.gmx.net [212.227.17.20]) by sourceware.org (Postfix) with ESMTPS id 40BE43858C2C; Thu, 9 Sep 2021 19:38:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 40BE43858C2C X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [79.251.10.127] ([79.251.10.127]) by web-mail.gmx.net (3c-app-gmx-bs71.server.lan [172.19.170.216]) (via HTTP); Thu, 9 Sep 2021 21:38:11 +0200 MIME-Version: 1.0 Message-ID: To: fortran , gcc-patches Subject: [PATCH, committed] PR fortran/98490 - Unexpected out of bounds in array constructor with implied do loop Date: Thu, 9 Sep 2021 21:38:11 +0200 Importance: normal Sensitivity: Normal X-Priority: 3 X-Provags-ID: V03:K1:+FUmkT+mkyUoEop3Y4KLeGIJbgpN4GGLdTiNxiSe8mKGtTKS12S8MX4cbDaYYkX1MzAtw 91EtvsCFEVJicUmFNPTKmG/yl96S/4ij+mz+7H795uPF/pkYcI3DqxXKhpPpgkRu4PV8Ar3qELyh xgFUpVCxSVZoVgkeqVqYvRseFYrDq+O39b9dOugXBuYHt+t2UVh2ov2n4RnMKxjcyjLwHLVJpy7C OXW1YHf8acJbq9y+vTHpWsvjtRfU1SSWVDiPgwggrRSXY5Tajq3yUZyBNcVbFqB+2YOFaG9gRQuJ D8= X-UI-Out-Filterresults: notjunk:1;V03:K0:EvQEHzLqYn4=:8FSVs0MS6DsQvtXigU4ZAU vOTd+w1ax3RXqB7HZHJJFs7Nfu1B2aV3me3l1AnPop+kecL1jiyMd7+FeK6aQpk9iQSKjHpat bsVfghbRp3QgK9Wgs5vHfHzYVU0p3HWpjGoPFhTV31BeQO8aTXUepb8wBt11T9KMgF61fIFN6 OoGFe08yYx0MReYsnvQ1j6YCeUBMXe9cTwe+HFmWZPM/r3501RWD6K3HxDEIYLM8oVX+vuZ2Z m5bW2MNBzO4/34XGbgOf9+ccLOUHYl9X8WEJkYenotWwl1WQiXyQrNIDYY+Yf3Qg7hz0RiUbc x+WuebVnsabrHVYFIHq7nLcz7RYYOdWjvTRHYGq364u3q0jvwMiiTfUrJk6f6kUILHt9Sklyt JCPueQTDTcovZqvlUHXj+0l3EoTMLbqc9TIcq64fcuPyJUHbPlUeuVwsLfRal+y4vTpSo1WlN lGsVAtSJLM9x88pvw49iuUmvPbrpTI3m6/DmBxBJG/JLrwZvRedE2vXBBduSFySh1YXw5p4WO tXeDExuLTJXJ7PKiN7GxHt3C4mnsqb5rSCtp+F9MKdgc+CenEgmCRoFsuoyq+1D/qi7cnt7FF io6lCOCWGnME+VW9AF3ooL8jBf7piqlt0tqX7LqjVhrgVAZt/3/dSSYEzOmfil1I++siV53rG OEO7FEpzixbAUzi/7scZeJCyWPTF7VIcgVgfiaiCn8mI2j6dEWSQsQkG7f6zQnnCvUU2b6S1m RkmZL/dMpgSX4ncO9LQ9He9rqzBqecCtBzFlzLEjJsZ5q1yREri6WagG1LCSDZ6ACEcJ25qcp De4pN+t X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, FREEMAIL_FROM, GIT_PATCH_0, 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-Patchwork-Original-From: Harald Anlauf via Gcc-patches From: Harald Anlauf Reply-To: Harald Anlauf Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" The following patch by Steve was lingering in the PR for some time. For an implied do loop within an array constructor we would generate an additional, bogus substring bounds check for the index variable before it actually became defined. The check thus depended on the previous value of the index variable before the implied do loop. The solution was to omit that early check. There is still a remaining check on the index variable, verified by manual inspection and by counting the substring bounds checks in the dump tree. Regtested on x86_64-pc-linux-gnu. Committed after discussion with Steve. The patch had also previously been approved by Paul. Thanks, Harald Fortran - out of bounds in array constructor with implied do loop gcc/fortran/ChangeLog: PR fortran/98490 * trans-expr.c (gfc_conv_substring): Do not generate substring bounds check for implied do loop index variable before it actually becomes defined. gcc/testsuite/ChangeLog: PR fortran/98490 * gfortran.dg/bounds_check_23.f90: New test. diff --git a/gcc/fortran/trans-expr.c b/gcc/fortran/trans-expr.c index c4291cce079..18d665192f0 100644 --- a/gcc/fortran/trans-expr.c +++ b/gcc/fortran/trans-expr.c @@ -2630,7 +2630,9 @@ gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind, if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp)) end.expr = gfc_evaluate_now (end.expr, &se->pre); - if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) + if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) + && (ref->u.ss.start->symtree + && !ref->u.ss.start->symtree->n.sym->attr.implied_index)) { tree nonempty = fold_build2_loc (input_location, LE_EXPR, logical_type_node, start.expr, diff --git a/gcc/testsuite/gfortran.dg/bounds_check_23.f90 b/gcc/testsuite/gfortran.dg/bounds_check_23.f90 new file mode 100644 index 00000000000..8de90c77c01 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/bounds_check_23.f90 @@ -0,0 +1,18 @@ +! { dg-do run } +! { dg-options "-fcheck=bounds -fdump-tree-original" } +! PR fortran/98490 - out of bounds in array constructor with implied do loop + +program test + implicit none + call sub('Lorem ipsum') +contains + subroutine sub( text ) + character(len=*), intent(in) :: text + character(len=1), allocatable :: c(:) + integer :: i + c = [ ( text(i:i), i = 1, len(text) ) ] + if (c(1) /= 'L') stop 1 + end subroutine sub +end program test + +! { dg-final { scan-tree-dump-times "Substring out of bounds:" 2 "original" } }