From patchwork Wed Jul 26 19:33:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Harald Anlauf X-Patchwork-Id: 1813442 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.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=ipADwq0C; dkim-atps=neutral Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RB3wx4xSxz1yY5 for ; Thu, 27 Jul 2023 05:33:53 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9251E3856976 for ; Wed, 26 Jul 2023 19:33:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9251E3856976 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1690400031; bh=IGmXH0s/4X+16obGDkABM3lvGxndfwzh+CEOVPfy2ts=; h=To:Subject:Date:References:In-Reply-To:Cc:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=ipADwq0C35O3dvVmXEUTfhFHQr38iGQ/svohEIZYRJWnpnWK6KXjkliSlGaM/KhZW q4woRhrxZoUbVckmEhnjAsPMcgI/QkP8rTsqNoEhj1O1cYAqEQWT3uoqSyCC3H4OdY AFlXPyKe624wnqzKd8HIsn2nmb1O2Peqbc8hlutQ= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from ciao.gmane.io (ciao.gmane.io [116.202.254.214]) by sourceware.org (Postfix) with ESMTPS id 71B66385AFA0 for ; Wed, 26 Jul 2023 19:33:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 71B66385AFA0 Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1qOkGH-0003nb-6A for gcc-patches@gcc.gnu.org; Wed, 26 Jul 2023 21:33:29 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: gcc-patches@gcc.gnu.org Subject: [PATCH, v3] Fortran: diagnose strings of non-constant length in DATA statements [PR68569] Date: Wed, 26 Jul 2023 21:33:22 +0200 Message-ID: <6e5a6a56-776a-1994-ffd3-f57e9d40ee20@gmx.de> References: Mime-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.12.0 Content-Language: en-US In-Reply-To: Cc: fortran@gcc.gnu.org X-Spam-Status: No, score=-8.9 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" I am going to get the brown bag for today... This is now the right corrected patch. Sorry for all the noise! Harald Am 26.07.23 um 21:17 schrieb Harald Anlauf via Gcc-patches: > Dear all, > > the original submission missed the adjustments of the expected > patterns of 2 tests.  This is corrected in the new attachments. > > Am 26.07.23 um 21:10 schrieb Harald Anlauf via Gcc-patches: >> Dear all, >> >> the attached patch fixes an ICE-on-invalid after use of strings of >> non-constant length in DATA statements. >> >> Regtested on x86_64-pc-linux-gnu.  OK for mainline? >> >> Thanks, >> Harald >> > > Thanks, > Harald > From d872b8ffc121fd57d47aa7d3d12d9ba86389f092 Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Wed, 26 Jul 2023 21:12:45 +0200 Subject: [PATCH] Fortran: diagnose strings of non-constant length in DATA statements [PR68569] gcc/fortran/ChangeLog: PR fortran/68569 * resolve.cc (check_data_variable): Do not accept strings with deferred length or non-constant length in a DATA statement. Reject also substrings of string variables of non-constant length. gcc/testsuite/ChangeLog: PR fortran/68569 * gfortran.dg/data_char_4.f90: Adjust expected diagnostic. * gfortran.dg/data_char_5.f90: Likewise. * gfortran.dg/data_char_6.f90: New test. --- gcc/fortran/resolve.cc | 22 ++++++++++++++++++- gcc/testsuite/gfortran.dg/data_char_4.f90 | 2 +- gcc/testsuite/gfortran.dg/data_char_5.f90 | 8 +++---- gcc/testsuite/gfortran.dg/data_char_6.f90 | 26 +++++++++++++++++++++++ 4 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/data_char_6.f90 diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc index f7cfdfc133f..3cd470ddcca 100644 --- a/gcc/fortran/resolve.cc +++ b/gcc/fortran/resolve.cc @@ -16771,7 +16771,6 @@ check_data_variable (gfc_data_variable *var, locus *where) return false; ar = NULL; - mpz_init_set_si (offset, 0); e = var->expr; if (e->expr_type == EXPR_FUNCTION && e->value.function.isym @@ -16838,8 +16837,24 @@ check_data_variable (gfc_data_variable *var, locus *where) "attribute", ref->u.c.component->name, &e->where); return false; } + + /* Reject substrings of strings of non-constant length. */ + if (ref->type == REF_SUBSTRING + && ref->u.ss.length + && ref->u.ss.length->length + && !gfc_is_constant_expr (ref->u.ss.length->length)) + goto bad_charlen; } + /* Reject strings with deferred length or non-constant length. */ + if (e->ts.type == BT_CHARACTER + && (e->ts.deferred + || (e->ts.u.cl->length + && !gfc_is_constant_expr (e->ts.u.cl->length)))) + goto bad_charlen; + + mpz_init_set_si (offset, 0); + if (e->rank == 0 || has_pointer) { mpz_init_set_ui (size, 1); @@ -16967,6 +16982,11 @@ check_data_variable (gfc_data_variable *var, locus *where) mpz_clear (offset); return t; + +bad_charlen: + gfc_error ("Non-constant character length at %L in DATA statement", + &e->where); + return false; } diff --git a/gcc/testsuite/gfortran.dg/data_char_4.f90 b/gcc/testsuite/gfortran.dg/data_char_4.f90 index ed0782ce8a0..fa5e0a0134a 100644 --- a/gcc/testsuite/gfortran.dg/data_char_4.f90 +++ b/gcc/testsuite/gfortran.dg/data_char_4.f90 @@ -4,7 +4,7 @@ program p character(l) :: c(2) ! { dg-error "must have constant character length" } - data c /'a', 'b'/ + data c /'a', 'b'/ ! { dg-error "Non-constant character length" } common c end diff --git a/gcc/testsuite/gfortran.dg/data_char_5.f90 b/gcc/testsuite/gfortran.dg/data_char_5.f90 index ea26687e3d5..7556e63c01b 100644 --- a/gcc/testsuite/gfortran.dg/data_char_5.f90 +++ b/gcc/testsuite/gfortran.dg/data_char_5.f90 @@ -4,12 +4,12 @@ subroutine sub () integer :: ll = 4 block - character(ll) :: c(2) ! { dg-error "non-constant" } - data c /'a', 'b'/ + character(ll) :: c(2) + data c /'a', 'b'/ ! { dg-error "Non-constant character length" } end block contains subroutine sub1 () - character(ll) :: d(2) ! { dg-error "non-constant" } - data d /'a', 'b'/ + character(ll) :: d(2) + data d /'a', 'b'/ ! { dg-error "Non-constant character length" } end subroutine sub1 end subroutine sub diff --git a/gcc/testsuite/gfortran.dg/data_char_6.f90 b/gcc/testsuite/gfortran.dg/data_char_6.f90 new file mode 100644 index 00000000000..4e32c647d4d --- /dev/null +++ b/gcc/testsuite/gfortran.dg/data_char_6.f90 @@ -0,0 +1,26 @@ +! { dg-do compile } +! PR fortran/68569 - ICE with automatic character object and DATA +! Contributed by G. Steinmetz + +subroutine s1 (n) + implicit none + integer, intent(in) :: n + character(n) :: x + data x /'a'/ ! { dg-error "Non-constant character length" } +end + +subroutine s2 (n) + implicit none + integer, intent(in) :: n + character(n) :: x + data x(1:1) /'a'/ ! { dg-error "Non-constant character length" } +end + +subroutine s3 () + implicit none + type t + character(:) :: c ! { dg-error "must be a POINTER or ALLOCATABLE" } + end type t + type(t) :: tp + data tp%c /'a'/ ! { dg-error "Non-constant character length" } +end -- 2.35.3