From patchwork Thu Oct 17 14:30:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Simon Martin X-Patchwork-Id: 1998614 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=nasilyan.com header.i=@nasilyan.com header.a=rsa-sha256 header.s=tey23rxsjton5kop5bydp3vc5ylkyjkg header.b=nwK6D4RS; dkim=pass (1024-bit key; unprotected) header.d=amazonses.com header.i=@amazonses.com header.a=rsa-sha256 header.s=uku4taia5b5tsbglxyj6zym32efj7xqv header.b=AWZBYLMm; dkim-atps=neutral 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=patchwork.ozlabs.org) 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 (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XTqz45Bfbz1xw2 for ; Fri, 18 Oct 2024 01:31:40 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C65CF3858C32 for ; Thu, 17 Oct 2024 14:31:38 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from a2-137.smtp-out.eu-west-1.amazonses.com (a2-137.smtp-out.eu-west-1.amazonses.com [54.240.2.137]) by sourceware.org (Postfix) with ESMTPS id B3EBE3858C53 for ; Thu, 17 Oct 2024 14:31:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B3EBE3858C53 Authentication-Results: sourceware.org; dmarc=fail (p=quarantine dis=none) header.from=nasilyan.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=eu-west-1.amazonses.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B3EBE3858C53 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=54.240.2.137 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729175477; cv=none; b=O2S2SVF6fhLTrSewK1dMktR63NZrYkDysK491NMXx6rvmxFPaaKYc4J4hk+cuGlP3sugEYEvwhUD7ORDsU6Eu9bSXh6RlRWL2j1Sx5r8SUKr+deerc+ZIW5RNay1g1VGFif44VlTpcTAZcNpyFCOGct8c08UmRp1JWFF/YG8OB0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729175477; c=relaxed/simple; bh=iv22XZhReqsdrx0w9FDs8ogPTgYDFEQCya662F7ae4A=; h=DKIM-Signature:DKIM-Signature:Subject:From:To:Date:Mime-Version: Message-ID; b=SQtC6EgoFLDp+u5a2UtwUVI3rvO2QTwHq6WtrUPr8h7/EHaSsdqnrnpR9S4/4XoAuoXNvs1CirrlUla+c+Rv62IjZCYnaI1L6CcNRzIchWjYtBL/SBpYn6MVmuefxn6TzWvnhjI9/HFruMSDDFZLSWAs7IFgwX6zj7MEHD/dtug= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple; s=tey23rxsjton5kop5bydp3vc5ylkyjkg; d=nasilyan.com; t=1729175440; h=Subject:From:To:Cc:Date:Mime-Version:Content-Type:In-Reply-To:References:Message-Id; bh=iv22XZhReqsdrx0w9FDs8ogPTgYDFEQCya662F7ae4A=; b=nwK6D4RS0J0oppsG2H29ZgWAzUDeJZahHkK7g8T9j2jMV+uvDR1e7MN5afMWIL+i FxMneK5qabmR49TyMlPtcr8mp0y1J7XgDQYHgsON87sLb6VE1KSRIk+dPdf3d/61ZgP uiRliiryiDv7D0uVtANsdRWDCmk3bHPXlwRjDO2A= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple; s=uku4taia5b5tsbglxyj6zym32efj7xqv; d=amazonses.com; t=1729175440; h=Subject:From:To:Cc:Date:Mime-Version:Content-Type:In-Reply-To:References:Message-Id:Feedback-ID; bh=iv22XZhReqsdrx0w9FDs8ogPTgYDFEQCya662F7ae4A=; b=AWZBYLMm1HYSUvJ2qDVUGU8uZTHhVF/a3KB2LD8WRqIZq9ucdOQsKAxcyPhe5v2X PoM2MPl0xO6IXuyDgcYmd5loi4xdUsg8RD8p9r+PmhKIDK6ouvYypLpCy2IUP04IM87 Eoh/3qUgEnDfPYM63/ETNdN7Ow5fHgv4CBiOS/rY= Subject: [PATCH v2] c++: Fix crash during NRV optimization with invalid input [PR117099, PR117129] From: =?utf-8?q?Simon_Martin?= To: =?utf-8?q?gcc-patches=40gcc=2Egnu=2Eorg?= Cc: =?utf-8?q?jason=40redhat=2Ecom?= Date: Thu, 17 Oct 2024 14:30:40 +0000 Mime-Version: 1.0 In-Reply-To: <0102019296d5f0d6-503071f0-4f6b-4ed0-80b6-9cbf5ebf7a6c-000000@eu-west-1.amazonses.com> References: <20241016193733.77124-1-simon@nasilyan.com> <0102019296d5f0d6-503071f0-4f6b-4ed0-80b6-9cbf5ebf7a6c-000000@eu-west-1.amazonses.com> X-Mailer: Amazon WorkMail Thread-Index: AQHbIALZQdWaZDJoQf2wbIryexCO7QAnknzm Thread-Topic: [PATCH v2] c++: Fix crash during NRV optimization with invalid input [PR117099, PR117129] X-Original-Mailer: MailMate (1.13.2r5673) X-Wm-Sent-Timestamp: 1729175439 Message-ID: <010201929ae34be2-65ab82c7-d542-481d-959d-05fac1213d0c-000000@eu-west-1.amazonses.com> Feedback-ID: ::1.eu-west-1.b24dn6frgCi6dh20skzbuMRr7UL8M6Soir/3ogtEjHQ=:AmazonSES X-SES-Outgoing: 2024.10.17-54.240.2.137 X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Hi, The issue reported in PR117129 is pretty similar to the one in PR117099, so here’s an updated version of the patch that fixes both issues, by ensuring that finish_return_expr sets current_function_return_value to error_mark_node in case of error with said return value. Successfully tested on x86_64-pc-linux-gnu. OK for trunk? Thanks, Simon From ff01d18d97893ef65259f9063794945d5062cf4e Mon Sep 17 00:00:00 2001 From: Simon Martin Date: Wed, 16 Oct 2024 15:47:12 +0200 Subject: [PATCH] c++: Fix crash during NRV optimization with invalid input [PR117099, PR117129] PR117099 and PR117129 are ICEs upon invalid code that happen when NRVO is activated, and both due to the fact that we don't consistently set current_function_return_value to error_mark_node upon error in finish_return_expr. This patch fixes this inconsistency which fixes both cases since we skip calling finalize_nrv when current_function_return_value is error_mark_node. Successfully tested on x86_64-pc-linux-gnu. PR c++/117099 PR c++/117129 gcc/cp/ChangeLog: * typeck.cc (check_return_expr): Upon error, set current_function_return_value to error_mark_node. gcc/testsuite/ChangeLog: * g++.dg/parse/crash77.C: New test. * g++.dg/parse/crash78.C: New test. --- gcc/cp/typeck.cc | 11 ++++++++--- gcc/testsuite/g++.dg/parse/crash77.C | 14 ++++++++++++++ gcc/testsuite/g++.dg/parse/crash78.C | 15 +++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/parse/crash77.C create mode 100644 gcc/testsuite/g++.dg/parse/crash78.C diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index 71d879abef1..485e8b347bb 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -11232,8 +11232,9 @@ check_return_expr (tree retval, bool *no_warning, bool *dangling) if (functype != error_mark_node) permerror (input_location, "return-statement with no value, in " "function returning %qT", valtype); - /* Remember that this function did return. */ + /* Remember that this function returns an error. */ current_function_returns_value = 1; + retval = error_mark_node; /* And signal caller that TREE_NO_WARNING should be set on the RETURN_EXPR to avoid control reaches end of non-void function warnings in tree-cfg.cc. */ @@ -11442,9 +11443,13 @@ check_return_expr (tree retval, bool *no_warning, bool *dangling) tf_warning_or_error); retval = convert (valtype, retval); - /* If the conversion failed, treat this just like `return;'. */ + /* If the conversion failed, treat this just like + `return ;'. */ if (retval == error_mark_node) - return retval; + { + current_function_return_value = error_mark_node; + return retval; + } /* We can't initialize a register from a AGGR_INIT_EXPR. */ else if (! cfun->returns_struct && TREE_CODE (retval) == TARGET_EXPR diff --git a/gcc/testsuite/g++.dg/parse/crash77.C b/gcc/testsuite/g++.dg/parse/crash77.C new file mode 100644 index 00000000000..8c3dddc13d2 --- /dev/null +++ b/gcc/testsuite/g++.dg/parse/crash77.C @@ -0,0 +1,14 @@ +// PR c++/117099 +// { dg-do "compile" } + +struct X { + ~X(); +}; + +X test(bool b) { + { + X x; + return x; + } + if (!(b)) return; // { dg-error "return-statement with no value" } +} diff --git a/gcc/testsuite/g++.dg/parse/crash78.C b/gcc/testsuite/g++.dg/parse/crash78.C new file mode 100644 index 00000000000..08d62af39c6 --- /dev/null +++ b/gcc/testsuite/g++.dg/parse/crash78.C @@ -0,0 +1,15 @@ +// PR c++/117129 +// { dg-do "compile" { target c++11 } } + +struct Noncopyable { + Noncopyable(); + Noncopyable(const Noncopyable &) = delete; // { dg-note "declared here" } + virtual ~Noncopyable(); +}; +Noncopyable nrvo() { + { + Noncopyable A; + return A; // { dg-error "use of deleted function" } + // { dg-note "display considered" "" { target *-*-* } .-1 } + } +}