From patchwork Wed Dec 7 20:57:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harald Anlauf X-Patchwork-Id: 31050 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp400263wrr; Wed, 7 Dec 2022 12:58:18 -0800 (PST) X-Google-Smtp-Source: AA0mqf6JwgMnzrfk1JG5OVm8wNOPGjgWxfViutsUPQhcpaswTT6T5remC8KKPfa34xn/575TVI4I X-Received: by 2002:a17:906:99cd:b0:7c1:10d9:8897 with SMTP id s13-20020a17090699cd00b007c110d98897mr6372304ejn.660.1670446698231; Wed, 07 Dec 2022 12:58:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670446698; cv=none; d=google.com; s=arc-20160816; b=G1xeIBee7iBVCI7lILyhYoMd+LLmHpZzFt92EmHgxECRoZzOLNgynaqPYVThlyAx/F 0ncwUePGeMenVIZrajQ4L6zU4E8jNOb3N1Ia+ZnRPeYJX5l9zjjTF6POvWYkr2ASseDN e5Lx6cov76hE/TlxqIiqw1OzIjJg//dOehMKqJz27XneG6BPnfqjUI3ldqFs40zO8qx6 /BeCxZBHZkOXimvnRNk5V1HrCmxDpE6rU9FEp9sR1jaywOdt+ySC243w6pWF4PMHHj3N 4Ze9AjCTnVkDLiBI2Ocpp/a0Ey9sT/Li8vQCdY5jC1VIETJshhn5i+ylEKP9carf7fIt 4Y/A== 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=kz9AT7mkbEgho1mqjrRA5cxzopgnnilkSvEaX1xWJgs=; b=RtypIPe4MixEppvduhi6f1/9kEsw73tJBKhSjpN7aLIhPXjK0iJrjTJazik8DqRZgf jN13mcq6k8l36JR3xr8HCYWTmwoKnDDxphl27T2rIb3xQjfpH6+/u04AI1RIBKNH4+uW jts/TYmslHCpU44n27f3uFJp1Neqzywemo4xuPIB0qGydhvHDltY460xW7eOiO39Hx+3 72BSfEkmAo1DDrDSAmvIvl/q5SbtumiMjNGPelLy5DzOX95VluAjSuRLE85RsIR72j4T W5b45HrYsN08zmDHmjz8Sca85E3Uj+fbEbBC3vZAt6EddDPo9KuseHit0RwPjVteiGFO s31Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=BgLeGVNx; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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. [8.43.85.97]) by mx.google.com with ESMTPS id w17-20020aa7cb51000000b0046cee3b0cc3si4882579edt.371.2022.12.07.12.58.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Dec 2022 12:58:18 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=BgLeGVNx; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 57DE9390CE81 for ; Wed, 7 Dec 2022 20:58:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 57DE9390CE81 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1670446688; bh=kz9AT7mkbEgho1mqjrRA5cxzopgnnilkSvEaX1xWJgs=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=BgLeGVNxv1w+J/GaFpDCTSqIIafd8eP53wqgmxbgUYoxeAR3ETZhVAbgvvRWPRX9Y fViD2BH/B2UFowQ6gyRxOMbzUoAZUffexVCXWO+opGp0sRTdKkn0Fn9LMrttLWmajK 2lnNG/TSJ1sxS5W10QpBVxhUknSsHBwYfH2ke8KE= 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.15.15]) by sourceware.org (Postfix) with ESMTPS id 9942F3858412; Wed, 7 Dec 2022 20:57:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9942F3858412 X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [93.207.90.66] ([93.207.90.66]) by web-mail.gmx.net (3c-app-gmx-bs03.server.lan [172.19.170.52]) (via HTTP); Wed, 7 Dec 2022 21:57:20 +0100 MIME-Version: 1.0 Message-ID: To: fortran , gcc-patches Subject: [PATCH] Fortran: handle zero-sized arrays in ctors with typespec [PR108010] Date: Wed, 7 Dec 2022 21:57:20 +0100 Importance: normal Sensitivity: Normal X-Priority: 3 X-Provags-ID: V03:K1:I0q2IjNcSG0T9xzgRWtyUXjUHd4i7GDOxz9UPhfCPUBvOr8YLvxfFeNuBRPcfDTAN2XB4 /KNeTHJ5DH/8GGuC8s0ljsX2fMu/AWg6E5VJ6s6yXc/gJ1Z5f1pyfxnqrfq3KayfF5XYQXYzi+ah viyKHIblGSV5IQVJQlpo9w0eU7VADbHI7P5OOC0RxpCIMyUN9hKUr351VHa6f5QqCr/ze8VkDCG3 RsFVOEP+hTXibVV62V9Jgyw8fAHqdLHcSVjekgM5M9dWyiaTWAD09ZZl8ipTdX6oqOQv8fmEpqy1 ZA= UI-OutboundReport: notjunk:1;M01:P0:zCeflCcWgdI=;5DUXFsHoi9fkD4DoO5m0Dm7wqYT CNE3adNq0EImboS5AILti2DrNpr7wkF5x/jKjcFmndpmxNJ7tlyzGDllFSKLV06bQfOUkQErF jQBghcwiWHnElV5VrYVUsNDdZzT7d8Hf5duyTdCqYsdgj/ObvtF3dkluhuUn2di/UTRPRdF3B UiTe05hsOwMjfK9wVAFYPDAMOScJWMcBSEBKGxFgpHPsAvXZMbnsa7zbkEK/2aCzbrukpN65g JkTcKAi5PrLPV2bDgcwUmNRR2q0L07mlTA/fhKcRLzKLI7AbT7UWRZjIKOOeODt8qM9BxSb8M Tu748QoPqIU7gq70psN4d9MJ/tWx2UoD11O5WQmXd3uLEAJWoIZc7acE3nhZ99dRkQMnSM6TH iZI8p2kWXA/WihwNo3RTyUdPKYYohjgKpHtkdN5KA7KUCHJBaP27zwOkPcolV67cMTqFT2cjX V50jwGAgL/cclu1ZYEvsahMflb3vkyidyB/VLXKvGZBPcWBbT0vc6g3GD6FEvMIQj7T3N/TNm Fjp5+eLjtEgp/byLqH1eltWNLSK5qGKBhn8UtsAc2IrvVF91iE4+dpobWGd/QXVdjxMiaqEOX lwYbVzAEWMsLEG3vi0nOBtvyLlFHH8AVS4OmGtEOK6x8MwCihM8k6zoxPrdyO7vuzhLNHARAE L8OFyjeTAEMQl6FYHwcfclxSb/cTBLuzTVlAbiVxJqaTnPjeXQ9eR7A//bFrcNNqOBvYNDKbf uM0Y2LV9Nn5tK8QVmT0ZfHJiWva6hGr3qYEwuS+JE7o887MB+yQt/jppFhJML8LsaRc7nEtjF PSTfJ0g7ZqO4MDayyIuY7szw== X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, DKIM_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 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?1751590317076280688?= X-GMAIL-MSGID: =?utf-8?q?1751590317076280688?= Dear all, we need to be careful about zero-sized arrays in arithmetic reductions (unary & binary), as we otherwise may hit a NULL pointer dereference on valid code. The actual fix is straightforward, see attached patch. Regtested on x86_64-pc-linux-gnu. OK for mainline? Thanks, Harald From 02a8b7308d04dc84fb13b077bd3b2fe01e15c92e Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Wed, 7 Dec 2022 21:50:23 +0100 Subject: [PATCH] Fortran: handle zero-sized arrays in ctors with typespec [PR108010] gcc/fortran/ChangeLog: PR fortran/108010 * arith.cc (reduce_unary): Handle zero-sized arrays. (reduce_binary_aa): Likewise. gcc/testsuite/ChangeLog: PR fortran/108010 * gfortran.dg/pr108010.f90: New test. --- gcc/fortran/arith.cc | 24 ++++++++++-- gcc/testsuite/gfortran.dg/pr108010.f90 | 54 ++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/pr108010.f90 diff --git a/gcc/fortran/arith.cc b/gcc/fortran/arith.cc index c4ab75b401c..c0d12cfad9d 100644 --- a/gcc/fortran/arith.cc +++ b/gcc/fortran/arith.cc @@ -1342,8 +1342,16 @@ reduce_unary (arith (*eval) (gfc_expr *, gfc_expr **), gfc_expr *op, else { gfc_constructor *c = gfc_constructor_first (head); - r = gfc_get_array_expr (c->expr->ts.type, c->expr->ts.kind, - &op->where); + if (c == NULL) + { + /* Handle zero-sized arrays. */ + r = gfc_get_array_expr (op->ts.type, op->ts.kind, &op->where); + } + else + { + r = gfc_get_array_expr (c->expr->ts.type, c->expr->ts.kind, + &op->where); + } r->shape = gfc_copy_shape (op->shape, op->rank); r->rank = op->rank; r->value.constructor = head; @@ -1501,8 +1509,16 @@ reduce_binary_aa (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **), else { gfc_constructor *c = gfc_constructor_first (head); - r = gfc_get_array_expr (c->expr->ts.type, c->expr->ts.kind, - &op1->where); + if (c == NULL) + { + /* Handle zero-sized arrays. */ + r = gfc_get_array_expr (op1->ts.type, op1->ts.kind, &op1->where); + } + else + { + r = gfc_get_array_expr (c->expr->ts.type, c->expr->ts.kind, + &op1->where); + } r->shape = gfc_copy_shape (op1->shape, op1->rank); r->rank = op1->rank; r->value.constructor = head; diff --git a/gcc/testsuite/gfortran.dg/pr108010.f90 b/gcc/testsuite/gfortran.dg/pr108010.f90 new file mode 100644 index 00000000000..303b2b98220 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/pr108010.f90 @@ -0,0 +1,54 @@ +! { dg-do run } +! PR fortran/108010 - ICE in reduce_unary, reduce_binary_aa +! Contributed by G.Steinmetz + +program p + implicit none + print *, + [integer :: [real ::]] + print *, - [integer :: [real ::]] + print *, 1 + [integer :: [real ::]] + print *, 1 - [integer :: [real ::]] + print *, 2 * [integer :: [real ::]] + print *, - [real :: [real ::], 2] + print *, + [integer :: [real ::], 2] + print *, - [integer :: [real ::], 2] + print *, 1 + [integer :: [real ::], 2] + print *, 1 - [integer :: [real ::], 2] + print *, 2 * [integer :: [real ::], 2] + print *, [integer :: [real ::]] + [integer :: [real ::]] + print *, [integer :: [real ::]] - [integer :: [real ::]] + print *, [integer :: [real ::]] * [integer :: [real ::]] + print *, [integer :: [real ::], 2] + [real :: [real ::], 3] + print *, [integer :: [real ::], 2] - [real :: [real ::], 3] + print *, [integer :: [real ::], 2] * [real :: [real ::], 3] + + ! Validate type of resulting arrays + if (.not. is_int ([integer :: [real ::]] )) stop 1 + if (.not. is_int ([integer :: [real ::]] + [integer :: [real ::]])) stop 2 + if (.not. is_real([real :: [integer ::]] )) stop 3 + if (.not. is_real([real :: [integer ::]] + [real :: [integer ::]])) stop 4 + if (.not. is_real([real :: [integer ::]] + [integer :: [real ::]])) stop 5 + if (.not. is_real([integer :: [real ::]] + [real :: [integer ::]])) stop 6 + +contains + + logical function is_int (x) + class(*) :: x(:) + select type (x) + type is (integer) + is_int = .true. + class default + is_int = .false. + end select + end function is_int + + logical function is_real (x) + class(*) :: x(:) + select type (x) + type is (real) + is_real = .true. + class default + is_real = .false. + end select + end function is_real +end -- 2.35.3