public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Marek Polacek <polacek@redhat.com>
To: Jason Merrill <jason@redhat.com>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH v2] c++: Fix value-init crash in template [PR93676]
Date: Wed, 19 Feb 2020 21:15:00 -0000	[thread overview]
Message-ID: <20200219211523.GC3559@redhat.com> (raw)
In-Reply-To: <6eb452b2-c291-8a14-9e33-303a789db065@redhat.com>

On Fri, Feb 14, 2020 at 12:24:30AM +0100, Jason Merrill wrote:
> On 2/11/20 8:54 PM, Marek Polacek wrote:
> > Since <https://gcc.gnu.org/ml/gcc-patches/2015-02/msg00556.html> we
> > attempt to value-initialize in build_vec_init even when there's no
> > initializer but the type has a constexpr default constructor.  But
> > build_value_init doesn't work in templates, so I think let's avoid
> > this scenario; we'll go to the normal build_aggr_init path then.
> > 
> > Bootstrapped/regtested on x86_64-linux, ok for trunk and branches?
> > 
> > 	PR c++/93676 - value-init crash in template.
> > 	* init.c (build_vec_init): Don't perform value-init in a template.
> 
> Hmm, we really shouldn't even be calling build_vec_init in a template, that
> builds up a lot of garbage that we'll throw away at the end of build_new.

Ah, it's true that build_new will just creates a NEW_EXPR in a template and
doesn't use the result of build_new_1.  Unfortunately I can't just call
build_special_member_call like we do in build_new_1 since that crashes for
array types.  Maybe just return NULL_TREE then?  I was afraid we would miss
diagnostics but it seems to work.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

-- >8 --
Since <https://gcc.gnu.org/ml/gcc-patches/2015-02/msg00556.html> we
attempt to value-initialize in build_vec_init even when there's no
initializer but the type has a constexpr default constructor.  But
build_value_init doesn't work in templates, and build_vec_init
creates a lot of garbage that would not be used anyway, so don't
call it in a template.

	PR c++/93676 - value-init crash in template.
	* init.c (build_new_1): Don't call build_vec_init in a template.

	* g++.dg/cpp0x/nsdmi-template19.C: New test.
---
 gcc/cp/init.c                                 |  6 ++++++
 gcc/testsuite/g++.dg/cpp0x/nsdmi-template19.C | 13 +++++++++++++
 2 files changed, 19 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/nsdmi-template19.C

diff --git a/gcc/cp/init.c b/gcc/cp/init.c
index d480660445e..332a9b04679 100644
--- a/gcc/cp/init.c
+++ b/gcc/cp/init.c
@@ -3554,6 +3554,12 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
                 error ("parenthesized initializer in array new");
 	      return error_mark_node;
             }
+	  /* We shouldn't call build_vec_init in a template: it could call
+	     build_value_init which doesn't work in templates, and it would
+	     create a lot of garbage that would not be used anyway, so return
+	     a null tree and let build_new create a NEW_EXPR instead.  */
+	  if (processing_template_decl)
+	    return NULL_TREE;
 	  init_expr
 	    = build_vec_init (data_addr,
 			      cp_build_binary_op (input_location,
diff --git a/gcc/testsuite/g++.dg/cpp0x/nsdmi-template19.C b/gcc/testsuite/g++.dg/cpp0x/nsdmi-template19.C
new file mode 100644
index 00000000000..f3e2cb87fd6
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/nsdmi-template19.C
@@ -0,0 +1,13 @@
+// PR c++/93676 - value-init crash in template.
+// { dg-do compile { target c++11 } }
+
+struct P {
+  int x = 0;
+};
+
+template<class T>
+struct S {
+  S() { new P[2][2]; }
+};
+
+S<int> s;

base-commit: 58f2e59ad36ca05444cb0a57ad1f13cc58e52755
-- 
Marek Polacek • Red Hat, Inc. • 300 A St, Boston, MA

  reply	other threads:[~2020-02-19 21:15 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-11 19:55 [PATCH] " Marek Polacek
2020-02-13 23:24 ` Jason Merrill
2020-02-19 21:15   ` Marek Polacek [this message]
2020-02-20  0:13     ` [PATCH v2] " Jason Merrill
2020-02-20 16:52       ` [PATCH v3] " Marek Polacek
2020-02-24 22:16         ` Jason Merrill
2020-02-25 17:53           ` [PATCH v4] " Marek Polacek
2020-02-25 18:27             ` Jason Merrill
2020-02-25 18:55               ` [PATCH v5] " Marek Polacek
2020-02-25 19:34                 ` Marek Polacek
2020-02-26  4:26                 ` Jason Merrill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200219211523.GC3559@redhat.com \
    --to=polacek@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).