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: 126542 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a985:0:b0:3e4:2afc:c1 with SMTP id t5csp621222vqo; Wed, 26 Jul 2023 12:34:43 -0700 (PDT) X-Google-Smtp-Source: APBJJlH+iTlrj/RIa40i03mmNwQAhb6Pon5MT6Cx2qgsLQIMMHFk/e465MkKznAqI1G9koQ1o8gE X-Received: by 2002:a17:907:2bde:b0:99b:d599:5085 with SMTP id gv30-20020a1709072bde00b0099bd5995085mr107479ejc.64.1690400083567; Wed, 26 Jul 2023 12:34:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690400083; cv=none; d=google.com; s=arc-20160816; b=DTFEwBKCU/FjcIQqDq3YxPHeXZoXBj/EFTGtWHFgN2sNGTrm//zZNB7bD/ZHz7A9Ae uhtI038axvYAk+kftIDhwJPdz+FGsm+1j688sr95JLgdHFmv91v63q52EuHiwi2UMHFI FVwe/cfVjwn9fO9BRnxdtY9exq1KaHEgR/QjNvh7aUFCtQN/ey5Sxy5LED6n9LwrF5WT /2DbS9LjsBUhkx9P3H3Q7EuAud4ys7BIFIJVBuVM0ZET9VbbJRf3JR1T/2iSumUxDknz abQo7wpdyNbUTncG8T6Y9s4SlTpKsjE/iyHabJeqlpbKN4Slnxr0TWtk3FaTRsvhvVqJ Bj+Q== 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:cc:in-reply-to :content-language:user-agent:mime-version:references:message-id:date :subject:to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=IGmXH0s/4X+16obGDkABM3lvGxndfwzh+CEOVPfy2ts=; fh=g8kyxQIElImBhiBbwgQXGdn7aSIo8X0mDjSlnmPVQJA=; b=uW8NG7egrg/x9cmmnvZ6MuO9U8Ijw5ea0PxqPS5Z6UetX4ne4ifyKvKwbIFNjNZ0vb gguJUeuJ+18k+hdUCner0NjcGbE4WKvmavfBy9Qr13ddYAm/M/Q8/n3C3qYl/pMv2QXR hzVm46Ae89Ni54mKY0rgG2RRlAlqXwKwNmIHpW36SQjEKMQfd4bdB+a3uKYxgMu8XxYK 5Qi7jusc6jYicr04GAr9Ie4gD+4Gh5VYU6zdLxpjzemjsf8dU+hb1hHcOmxstEJisCPj j4ETcnpXtQMTY/tZD0SvS4vMFwsb2nbzshnivbUaoWLzcFR+2r8JPSLP/esX+Man+YDp 7dVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Mtt8Gwcr; 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id m11-20020a170906160b00b00988c5329e26si9938668ejd.574.2023.07.26.12.34.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jul 2023 12:34:43 -0700 (PDT) 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=Mtt8Gwcr; 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 7FC0C385C6F9 for ; Wed, 26 Jul 2023 19:34:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7FC0C385C6F9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1690400055; 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=Mtt8Gwcr4xOLFQq0tzSSEFyXbr+rtQ7lnyZ3u6Ua4qQYRPZAFRO0Wb06szItwfcf8 QQsIfj6Fc5A/qXXCEIFLJlfC0DgZLIgreMkQ1mV/wzGrfWFsyBQxCxGyBs+rLm9wY0 zytM4A7w4V/ZpJNTJW/+VFOQ0E63MarLaeb4QO5g= 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772511479392167940 X-GMAIL-MSGID: 1772512957920786237 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