From patchwork Mon Feb 20 20:42:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harald Anlauf X-Patchwork-Id: 59665 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1503751wrn; Mon, 20 Feb 2023 12:43:34 -0800 (PST) X-Google-Smtp-Source: AK7set90r77jpERtbmX6xrXyzReBwDNydfsCm6e3uGwqQ/f1dnEwCREwvTSWLaIKDE9WuWdOludh X-Received: by 2002:a05:6402:4413:b0:4ad:15d9:53b1 with SMTP id y19-20020a056402441300b004ad15d953b1mr1604887eda.9.1676925814585; Mon, 20 Feb 2023 12:43:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676925814; cv=none; d=google.com; s=arc-20160816; b=w7PsEkPrXMWzJvwaXUjP7RsbDKJK+D0LDGdttsY+Ahu/NeDvIf86AEczvGF3f8L1fH Bl70bU5AE2MYPVhBbKXyQt4oTtOkk/hsChikX+3SCYgaQ8O78YfzxBvA78oJbz5myr5/ dlAMAMaBhaYoGuZNH14XLJungZMwbZVG3J0Y8d6lhTUXfYE2CNUt3FaKrYnswfcEz8wl mW+XH+Z57D7MKcVdZzLE9BCXjh279Y/js5MeHWGOAnkyI+k9CmrzJ0i0gqYYmBV3a3Ke Mlg9K3UkY2DnD1BpmSIvax3i/xbTXK9QVlxPzZ0Ht/J+aca+j8Jp1CCXrdx6zGO4dN9t KEIQ== 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=GUGwq1sYgVzZetI4urvYkHDuMCMrX+VLFBN7IGFoedI=; b=nk1KacnIogDCSsKno7UaQ1NVdepciXBvL/KrPxzEs0lyZU6BIcWkCQxXDSE57pSkZY qBd6UivUDcArEf4UtbIihnYKu39Yxame5XAmf3FJ1BDCz3qFzUE51heol18+APVfAhme AvvEe1S0NWQ+orDhhsC3EubVn5fIJNR9xXL0MDSgJtkZyRICq0yf38gFNimlZrJACb6G GQ2/F8dTLdC0RJW03NuOYVFaCydK+ubu6Nur5ZiIGH0Kx4+POVBUVZ1Annur8MKGWSJs Sqmx7uCOQx8noAeyGk1xlCJXTlipKjloyyfYHursZa0TZjNZLQ8uDYRUNvS/+CBsUaxJ o+2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=fmGPT+sG; 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 x22-20020aa7dad6000000b004ab957caf1asi4791447eds.255.2023.02.20.12.43.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Feb 2023 12:43:34 -0800 (PST) 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=fmGPT+sG; 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 07AA0384FB4D for ; Mon, 20 Feb 2023 20:43:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 07AA0384FB4D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1676925810; bh=GUGwq1sYgVzZetI4urvYkHDuMCMrX+VLFBN7IGFoedI=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=fmGPT+sGDZ6AWfWr1KCw2lk0pOkqNWmnDELVqQmk3ysWZcoTrj3YN/HmRK8IK07v2 MSureTWwn2WnnBZfrLzb2NTm1o+60lUhRccb2Z3qvqTFTyvB+BQQvX9vgHfSDVUUyL dq5KXaZ99enF0hWx9E4huXPhXdVmRyHRJQHuyqvE= 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 856133858D20; Mon, 20 Feb 2023 20:42:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 856133858D20 X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [93.207.80.146] ([93.207.80.146]) by web-mail.gmx.net (3c-app-gmx-bap60.server.lan [172.19.172.130]) (via HTTP); Mon, 20 Feb 2023 21:42:42 +0100 MIME-Version: 1.0 Message-ID: To: fortran , gcc-patches Subject: [PATCH] Fortran: improve checking of character length specification [PR96025] Date: Mon, 20 Feb 2023 21:42:42 +0100 Importance: normal Sensitivity: Normal X-Priority: 3 X-Provags-ID: V03:K1:2zTYnoB5bC0d0G9Us9G2TlQIHEmyXxqhsICqDj3PLAZH2pVNwQ1RoUHb0rXsfKJvm7V01 ZucHHwVtr/0LCyFhU7p+cVyLsOST/vrY1C+9rGDeihvfKSInGh+5uGp9HZ7pixjywX1pHgfpiRmS THrLBG7oG5r5hnnuYYeHS+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13.0 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?1758384163017129215?= X-GMAIL-MSGID: =?utf-8?q?1758384163017129215?= Dear all, the attached patch fixes an ICE on invalid (non-integer) specification expressions for character length in function declarations. It appears that the error handling was already in place (mostly) and we need to essentially prevent run-on errors. Regtested on x86_64-pc-linux-gnu. OK for mainline? The PR is marked as a 10/11/12/13 regression, so I would like to backport this as far as it seems reasonable. Thanks, Harald From f581f63e206b54278c27a5c888c2566cb5077f11 Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Mon, 20 Feb 2023 21:28:09 +0100 Subject: [PATCH] Fortran: improve checking of character length specification [PR96025] gcc/fortran/ChangeLog: PR fortran/96025 * parse.cc (check_function_result_typed): Improve type check of specification expression for character length and return status. (parse_spec): Use status from above. * resolve.cc (resolve_fntype): Prevent use of invalid specification expression for character length. gcc/testsuite/ChangeLog: PR fortran/96025 * gfortran.dg/pr96025.f90: New test. --- gcc/fortran/parse.cc | 23 ++++++++++++++++------- gcc/fortran/resolve.cc | 4 +++- gcc/testsuite/gfortran.dg/pr96025.f90 | 11 +++++++++++ 3 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/pr96025.f90 diff --git a/gcc/fortran/parse.cc b/gcc/fortran/parse.cc index f5154d97ae8..47876a3833e 100644 --- a/gcc/fortran/parse.cc +++ b/gcc/fortran/parse.cc @@ -3974,21 +3974,30 @@ match_deferred_characteristics (gfc_typespec * ts) For return types specified in a FUNCTION prefix, the IMPLICIT rules of the scope are not yet parsed so this has to be delayed up to parse_spec. */ -static void +static bool check_function_result_typed (void) { gfc_typespec ts; gcc_assert (gfc_current_state () == COMP_FUNCTION); - if (!gfc_current_ns->proc_name->result) return; + if (!gfc_current_ns->proc_name->result) + return true; ts = gfc_current_ns->proc_name->result->ts; /* Check type-parameters, at the moment only CHARACTER lengths possible. */ /* TODO: Extend when KIND type parameters are implemented. */ if (ts.type == BT_CHARACTER && ts.u.cl && ts.u.cl->length) - gfc_expr_check_typed (ts.u.cl->length, gfc_current_ns, true); + { + /* Reject invalid type of specification expression for length. */ + if (ts.u.cl->length->ts.type != BT_INTEGER) + return false; + + gfc_expr_check_typed (ts.u.cl->length, gfc_current_ns, true); + } + + return true; } @@ -4097,8 +4106,8 @@ loop: if (verify_now) { - check_function_result_typed (); - function_result_typed = true; + if (check_function_result_typed ()) + function_result_typed = true; } } @@ -4111,8 +4120,8 @@ loop: case ST_IMPLICIT: if (!function_result_typed) { - check_function_result_typed (); - function_result_typed = true; + if (check_function_result_typed ()) + function_result_typed = true; } goto declSt; diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc index fb0745927ac..427f901a438 100644 --- a/gcc/fortran/resolve.cc +++ b/gcc/fortran/resolve.cc @@ -17419,7 +17419,9 @@ resolve_fntype (gfc_namespace *ns) } } - if (sym->ts.type == BT_CHARACTER) + if (sym->ts.type == BT_CHARACTER + && sym->ts.u.cl->length + && sym->ts.u.cl->length->ts.type == BT_INTEGER) gfc_traverse_expr (sym->ts.u.cl->length, sym, flag_fn_result_spec, 0); } diff --git a/gcc/testsuite/gfortran.dg/pr96025.f90 b/gcc/testsuite/gfortran.dg/pr96025.f90 new file mode 100644 index 00000000000..ce292bd9664 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/pr96025.f90 @@ -0,0 +1,11 @@ +! { dg-do compile } +! PR fortran/96025 - ICE in expr_check_typed_help +! Contributed by G.Steinmetz + +program p + print *, f() +contains + character(char(1)) function f() ! { dg-error "must be of INTEGER type" } + f = 'f' + end +end -- 2.35.3