From patchwork Wed May 17 18:52:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harald Anlauf X-Patchwork-Id: 95490 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1351346vqo; Wed, 17 May 2023 11:53:04 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5SiM6Hso94pfP+kEl5LXrW16kJcLwnZO4Gnq936Oo17wrXvE+sdw2OPe6q6QOl/jNzqm+n X-Received: by 2002:a17:907:724d:b0:96a:53e6:eab5 with SMTP id ds13-20020a170907724d00b0096a53e6eab5mr21552268ejc.41.1684349583573; Wed, 17 May 2023 11:53:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684349583; cv=none; d=google.com; s=arc-20160816; b=IpwEfZlQ7W0RGv+pHGkfKDtnzDvW3D0m9nTi/4Axb9pSziYW7raAlyqph2cOQug7R5 096ySUiS9XUuJCgCBHT47LUJbei1TzQFTZsRKwv+0ue9dyTHmPG/98zBNuGI1Y3J1y3O 0FFkc/YymUpMdeKg22tV02x9FPmpQgCdYp5WWGeQS29RhuQkn5RDx6zZh4pK27v+dEAS Cuxwwen99NMYX+9Apf0cJiPslqtQ5a1Dn2FLZFH055c9gAzKzxS0J0NnpmLhLXpC8MM6 141+Ns0V7Sf+53AkoAEj3Cf05pQh/pfj08qFxokmil9eHbIukfvxyxoYebloeB6evgPq aSWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:ui-outboundreport :sensitivity:importance:date:subject:to:message-id:mime-version :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=NM8rYzh37XSjlbqkUzkFupoTKM/Iew2htDoNhDzzVRg=; b=au1zIpfAckkXMSi3bNJiNZQ591tpg5+ZcWJo35eD42j1xYGFj7lNYovRHfbou6q3nD +FFm2dkmYDPAv7DI9frYC8xJocV014YodE6UQQ6AX6ieQGHXlX4jHblz1Ts6jCxZOxPr cKEXWRW1ixabsql20o95GjtFTGaaA53gRkfK/+w+0HiD6+jJscRWXJ0m2P8jyoIvpChm 4tG8pLZT5x+ZX3nYWsDvVuIRx3vqsmPx719E3/91E5mpwKRvITPMiB6sr8FYLWbMQJg0 6WHaN33nFJLngdEz8RdPwYOpSV5R712HapIxTD6zyA8tjgqakzV5zY4QKj7+FyiYIyNF VVqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=OFBOHRjL; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id bj2-20020a170906b04200b0095f90da23b6si14130746ejb.506.2023.05.17.11.53.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 May 2023 11:53:03 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=OFBOHRjL; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3E4AB3858C41 for ; Wed, 17 May 2023 18:53:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3E4AB3858C41 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1684349582; bh=NM8rYzh37XSjlbqkUzkFupoTKM/Iew2htDoNhDzzVRg=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=OFBOHRjLkfTQyUHAxszjAV/sj6aIDxV9xvLUId0a4DhmecjrxqSieWdv6qA3fCjDB KxwmWhyj/VqZBUhG0jKrQNdelamNIzUOn0eapYspX0gwOaMPZCIG+h6Zakaft1MeVk Rc6Cb8oyAW4+/yAIc0nQKC0aRZ0mSDA7y2twurzk= 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 413893858D1E; Wed, 17 May 2023 18:52:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 413893858D1E X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [79.251.12.34] ([79.251.12.34]) by web-mail.gmx.net (3c-app-gmx-bap55.server.lan [172.19.172.125]) (via HTTP); Wed, 17 May 2023 20:52:10 +0200 MIME-Version: 1.0 Message-ID: To: fortran , gcc-patches Subject: [PATCH] Fortran: set shape of initializers of zero-sized arrays [PR95374,PR104352] Date: Wed, 17 May 2023 20:52:10 +0200 Importance: normal Sensitivity: Normal X-Priority: 3 X-Provags-ID: V03:K1:w9BN7uBciAML9ZXDpVSlitx3dbqySmmYEgutXT8bR9ClZaG3fzdLDaTPcvBad3sPRM2+9 sQ7FkLX6dEzxs2+G0EaqXb8H7aMENv53r6aFXhl9fkoR1MjQgiL/o32G/UYXbz5S9GKpySOc2h7P Is85+1h2g+RA2LIXACrd6R+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IM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766168549023950335?= X-GMAIL-MSGID: =?utf-8?q?1766168549023950335?= Dear all, the attached patch is neat, because it fixes a bug by removing code ;-) When generating the initializer for a parameter array, we excepted the case of size 0, which however prevented the detection of array bounds violations and lead to ICEs in various places. The solution which removes the comparison for size > 0 also has the bonus that it fixes a minor memory leak for the size==0 case... Regtested on x86_64-pc-linux-gnu. OK for mainline? Thanks, Harald From 9d2995d2c1cf5708e3297fc7cffb5184d45a65cb Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Wed, 17 May 2023 20:39:18 +0200 Subject: [PATCH] Fortran: set shape of initializers of zero-sized arrays [PR95374,PR104352] gcc/fortran/ChangeLog: PR fortran/95374 PR fortran/104352 * decl.cc (add_init_expr_to_sym): Set shape of initializer also for zero-sized arrays, so that bounds violations can be detected later. gcc/testsuite/ChangeLog: PR fortran/95374 PR fortran/104352 * gfortran.dg/zero_sized_13.f90: New test. --- gcc/fortran/decl.cc | 3 +-- gcc/testsuite/gfortran.dg/zero_sized_13.f90 | 28 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/zero_sized_13.f90 diff --git a/gcc/fortran/decl.cc b/gcc/fortran/decl.cc index 9c4b40d4ac4..4c578d01ad4 100644 --- a/gcc/fortran/decl.cc +++ b/gcc/fortran/decl.cc @@ -2239,8 +2239,7 @@ add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus) && gfc_is_constant_expr (init) && (init->expr_type == EXPR_CONSTANT || init->expr_type == EXPR_STRUCTURE) - && spec_size (sym->as, &size) - && mpz_cmp_si (size, 0) > 0) + && spec_size (sym->as, &size)) { array = gfc_get_array_expr (init->ts.type, init->ts.kind, &init->where); diff --git a/gcc/testsuite/gfortran.dg/zero_sized_13.f90 b/gcc/testsuite/gfortran.dg/zero_sized_13.f90 new file mode 100644 index 00000000000..4035d458b32 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/zero_sized_13.f90 @@ -0,0 +1,28 @@ +! { dg-do compile } +! { dg-options "-w" } +! +! PR fortran/95374 +! PR fortran/104352 - Various ICEs for bounds violation with zero-sized arrays +! +! Contributed by G. Steinmetz + +program p + implicit none + integer :: i + integer, parameter :: a(0) = 0 + integer, parameter :: b(0:-5) = 0 + integer, parameter :: c(*) = [(a(i:i), i=0,0)] ! { dg-error "out of bounds" } + integer, parameter :: d(*) = [(b(i:i), i=1,1)] ! { dg-error "out of bounds" } + integer, parameter :: e(1) = [(a(i) , i=1,1)] ! { dg-error "out of bounds" } + integer, parameter :: f(1) = [(a(i:i), i=1,1)] ! { dg-error "out of bounds" } + integer :: g(1) = [(a(i:i), i=0,0)] ! { dg-error "out of bounds" } + integer :: h(1) = [(a(i:i), i=1,1)] ! { dg-error "out of bounds" } + print *, [(a(i:i), i=0,0)] ! { dg-error "out of bounds" } + print *, [(a(i:i), i=1,1)] ! { dg-error "out of bounds" } + print *, any (a(1:1) == 1) ! { dg-error "out of bounds" } + print *, all (a(0:0) == 1) ! { dg-error "out of bounds" } + print *, sum (a(1:1)) ! { dg-error "out of bounds" } + print *, iall (a(0:0)) ! { dg-error "out of bounds" } + print *, minloc (a(0:0),1) ! { dg-error "out of bounds" } + print *, dot_product(a(1:1),a(1:1)) ! { dg-error "out of bounds" } +end -- 2.35.3