diff mbox series

c++: Fix crash with template spec in different namespace [PR94255]

Message ID 20200418144920.1479035-1-polacek@redhat.com
State New
Headers show
Series c++: Fix crash with template spec in different namespace [PR94255] | expand

Commit Message

Marek Polacek April 18, 2020, 2:49 p.m. UTC
This is an ICE on invalid, because we're specializing S::foo in the
wrong namespace.  cp_parser_class_specifier_1 parses S::foo in M
and then it tries to push the nested-name-specifier of foo, which is
S.  By that, we're breaking the assumption of push_inner_scope that
the pushed scope must be a scope nested inside current scope: current
scope is M, but the namespace context of S is N, and N is not nested
in M, so we fell into an infinite loop in push_inner_scope_r.

(cp_parser_class_head called check_specialization_namespace which already
gave a permerror.)

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

	PR c++/94255
	* parser.c (cp_parser_class_specifier_1): Check that the scope is
	nested inside current scope before pushing it.

	* g++.dg/template/spec41.C: New test.
---
 gcc/cp/parser.c                        |  7 ++++++-
 gcc/testsuite/g++.dg/template/spec41.C | 17 +++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/template/spec41.C


base-commit: b57e1621eb76ba80c949ad098829aa8171a8c4ab

Comments

Jason Merrill April 20, 2020, 7:23 p.m. UTC | #1
On 4/18/20 10:49 AM, Marek Polacek wrote:
> This is an ICE on invalid, because we're specializing S::foo in the
> wrong namespace.  cp_parser_class_specifier_1 parses S::foo in M
> and then it tries to push the nested-name-specifier of foo, which is
> S.  By that, we're breaking the assumption of push_inner_scope that
> the pushed scope must be a scope nested inside current scope: current
> scope is M, but the namespace context of S is N, and N is not nested
> in M, so we fell into an infinite loop in push_inner_scope_r.
> 
> (cp_parser_class_head called check_specialization_namespace which already
> gave a permerror.)
> 
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

This doesn't seem to be a regresssion, so OK for stage 1.

> 	PR c++/94255
> 	* parser.c (cp_parser_class_specifier_1): Check that the scope is
> 	nested inside current scope before pushing it.
> 
> 	* g++.dg/template/spec41.C: New test.
> ---
>   gcc/cp/parser.c                        |  7 ++++++-
>   gcc/testsuite/g++.dg/template/spec41.C | 17 +++++++++++++++++
>   2 files changed, 23 insertions(+), 1 deletion(-)
>   create mode 100644 gcc/testsuite/g++.dg/template/spec41.C
> 
> diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
> index d2f3f853cb4..d4482ef5a90 100644
> --- a/gcc/cp/parser.c
> +++ b/gcc/cp/parser.c
> @@ -23858,7 +23858,12 @@ cp_parser_class_specifier_1 (cp_parser* parser)
>     if (nested_name_specifier_p)
>       {
>         scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
> -      old_scope = push_inner_scope (scope);
> +      /* SCOPE must be a scope nested inside current scope.  */
> +      if (is_nested_namespace (current_namespace,
> +			       decl_namespace_context (scope)))
> +	old_scope = push_inner_scope (scope); > +      else
> +	nested_name_specifier_p = false;
>       }
>     type = begin_class_definition (type);
>   
> diff --git a/gcc/testsuite/g++.dg/template/spec41.C b/gcc/testsuite/g++.dg/template/spec41.C
> new file mode 100644
> index 00000000000..249fde74c3a
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/template/spec41.C
> @@ -0,0 +1,17 @@
> +// PR c++/94255 - crash with template spec in different namespace.
> +// { dg-do compile { target c++11 } }
> +
> +namespace N {
> +  class S {
> +    template <typename> struct foo;
> +  };
> +  namespace M {
> +    using S = ::N::S;
> +  }
> +}
> +
> +namespace N {
> +  namespace M {
> +    template <> struct S::foo<int> {}; // { dg-error "specialization of" }
> +  }
> +}
> 
> base-commit: b57e1621eb76ba80c949ad098829aa8171a8c4ab
>
diff mbox series

Patch

diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index d2f3f853cb4..d4482ef5a90 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -23858,7 +23858,12 @@  cp_parser_class_specifier_1 (cp_parser* parser)
   if (nested_name_specifier_p)
     {
       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
-      old_scope = push_inner_scope (scope);
+      /* SCOPE must be a scope nested inside current scope.  */
+      if (is_nested_namespace (current_namespace,
+			       decl_namespace_context (scope)))
+	old_scope = push_inner_scope (scope);
+      else
+	nested_name_specifier_p = false;
     }
   type = begin_class_definition (type);
 
diff --git a/gcc/testsuite/g++.dg/template/spec41.C b/gcc/testsuite/g++.dg/template/spec41.C
new file mode 100644
index 00000000000..249fde74c3a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/spec41.C
@@ -0,0 +1,17 @@ 
+// PR c++/94255 - crash with template spec in different namespace.
+// { dg-do compile { target c++11 } }
+
+namespace N {
+  class S {
+    template <typename> struct foo;
+  };
+  namespace M {
+    using S = ::N::S;
+  }
+}
+
+namespace N {
+  namespace M {
+    template <> struct S::foo<int> {}; // { dg-error "specialization of" }
+  }
+}