From patchwork Thu Apr 30 16:03:29 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1280550 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: 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=niH0o18w; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49CgD44r49z9sSM for ; Fri, 1 May 2020 02:03:47 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0E460389853A; Thu, 30 Apr 2020 16:03:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0E460389853A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1588262624; bh=y9MXsyEqDA0p9zM2DLz69C/Kk1Pa0F0Fe4I/2x1dXeg=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=niH0o18wjJwSU1itldd2w9bKcQk5eA/+JE5554hJAZi01om/g9G7AF4qeX48jWlyb 4OhJ3t/Pz0St7IAFG2FIeFo/tntXKL5398JhA99gnbR7XW86GZsIwk2ZKsfJ8gWG26 c+KvPrxaLpLpQm3NboPw64XpXNNdFYqFLklrywX4= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by sourceware.org (Postfix) with ESMTP id C3CCF389853A for ; Thu, 30 Apr 2020 16:03:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org C3CCF389853A Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-225-H0GEPjU4MUqRhVyPhzk2YQ-1; Thu, 30 Apr 2020 12:03:39 -0400 X-MC-Unique: H0GEPjU4MUqRhVyPhzk2YQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9DB93835B5C for ; Thu, 30 Apr 2020 16:03:38 +0000 (UTC) Received: from pdp-11.redhat.com (ovpn-119-124.rdu2.redhat.com [10.10.119.124]) by smtp.corp.redhat.com (Postfix) with ESMTP id 134715D9F1; Thu, 30 Apr 2020 16:03:37 +0000 (UTC) To: Jason Merrill , GCC Patches Subject: [PATCH] c++: Parenthesized-init of aggregates accepts invalid code [PR94885] Date: Thu, 30 Apr 2020 12:03:29 -0400 Message-Id: <20200430160329.3143417-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-28.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" Here we have (conceptually *) something like struct B { }; struct D : B { }; D(0); // invalid and in C++20 the ()-initialization has created a { 0 } constructor that it tries to initialize an object of type D with. We should reject initializing an object of type B from 0, but we wrongly accept it because process_init_constructor_record skips initializers for empty bases/fields: if (DECL_SIZE (field) && integer_zerop (DECL_SIZE (field)) && !TREE_SIDE_EFFECTS (next)) /* Don't add trivial initialization of an empty base/field to the constructor, as they might not be ordered the way the back-end expects. */ continue; but here 'next' was error_mark_node, returned by massage_elt_init, so we wound up with { } which would validly value-initialize the object. [*] Usually digest_init in build_new_method_call_1 would detect this, but in this case the instance is is_dummy_object and we don't call digest just yet. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/10? PR c++/94885 * typeck2.c (process_init_constructor_record): Return PICFLAG_ERRONEOUS if an initializer element was erroneous. * g++.dg/cpp2a/paren-init26.C: New test. --- gcc/cp/typeck2.c | 6 +++++- gcc/testsuite/g++.dg/cpp2a/paren-init26.C | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/paren-init26.C base-commit: 448c89d590455ed4ab7abc40309b5cf8ec52d13d diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c index 56fd9bafa7e..9e5d145a6cd 100644 --- a/gcc/cp/typeck2.c +++ b/gcc/cp/typeck2.c @@ -1661,7 +1661,11 @@ process_init_constructor_record (tree type, tree init, int nested, int flags, ++idx; } } - if (next) + if (next == error_mark_node) + /* We skip initializers for empty bases/fields, so skipping an invalid + one could make us accept invalid code. */ + return PICFLAG_ERRONEOUS; + else if (next) /* Already handled above. */; else if (DECL_INITIAL (field)) { diff --git a/gcc/testsuite/g++.dg/cpp2a/paren-init26.C b/gcc/testsuite/g++.dg/cpp2a/paren-init26.C new file mode 100644 index 00000000000..0b98ebf43bb --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/paren-init26.C @@ -0,0 +1,14 @@ +// PR c++/94885 - paren-init of aggregates accepts invalid code. +// { dg-do compile { target c++2a } } + +template // { dg-error "could not convert" } +void foo(); + +struct base {}; +struct derived : base {}; + +void +bar() +{ + foo(); // { dg-error "no matching function" } +}