diff mbox series

c++: Parenthesized-init of aggregates accepts invalid code [PR94885]

Message ID 20200430160329.3143417-1-polacek@redhat.com
State New
Headers show
Series c++: Parenthesized-init of aggregates accepts invalid code [PR94885] | expand

Commit Message

Marek Polacek April 30, 2020, 4:03 p.m. UTC
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

Comments

Jason Merrill May 1, 2020, 8:29 p.m. UTC | #1
On 4/30/20 12:03 PM, Marek Polacek wrote:
> 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?

OK.

> 	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
> 
> 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 <typename T, typename = decltype(T(0))> // { dg-error "could not convert" }
> +void foo();
> +
> +struct base {};
> +struct derived : base {};
> +
> +void
> +bar()
> +{
> +  foo<derived>(); // { dg-error "no matching function" }
> +}
> 
> base-commit: 448c89d590455ed4ab7abc40309b5cf8ec52d13d
>
diff mbox series

Patch

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 <typename T, typename = decltype(T(0))> // { dg-error "could not convert" }
+void foo();
+
+struct base {};
+struct derived : base {};
+
+void
+bar()
+{
+  foo<derived>(); // { dg-error "no matching function" }
+}