public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: Fix value-init crash in template [PR93676]
@ 2020-02-11 19:55 Marek Polacek
  2020-02-13 23:24 ` Jason Merrill
  0 siblings, 1 reply; 11+ messages in thread
From: Marek Polacek @ 2020-02-11 19:55 UTC (permalink / raw)
  To: Jason Merrill, GCC Patches

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.

	* g++.dg/cpp0x/nsdmi-template19.C: New test.
---
 gcc/cp/init.c                                 |  2 +-
 gcc/testsuite/g++.dg/cpp0x/nsdmi-template19.C | 13 +++++++++++++
 2 files changed, 14 insertions(+), 1 deletion(-)
 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..c9c0f03c58b 100644
--- a/gcc/cp/init.c
+++ b/gcc/cp/init.c
@@ -4520,7 +4520,7 @@ build_vec_init (tree base, tree maxindex, tree init,
 
      We do need to keep going if we're copying an array.  */
 
-  if (try_const && !init)
+  if (try_const && !init && !processing_template_decl)
     /* With a constexpr default constructor, which we checked for when
        setting try_const above, default-initialization is equivalent to
        value-initialization, and build_value_init gives us something more
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: 7a775242ea296849a34ce27de179eaaec411e880
-- 
Marek Polacek • Red Hat, Inc. • 300 A St, Boston, MA

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2020-02-26  4:26 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-11 19:55 [PATCH] c++: Fix value-init crash in template [PR93676] Marek Polacek
2020-02-13 23:24 ` Jason Merrill
2020-02-19 21:15   ` [PATCH v2] " Marek Polacek
2020-02-20  0:13     ` 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

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).