Bootstrapped and regtested on x86_64-pc-linux-gnu, pushing as obvious.

-- >8 --

When we enter cp_parser_explicit_template_declaration with the following
tokens being 'template <>', we never parse a parameter list and so with
-fconcepts we crash dereferencing a null pointer.  This can currently
only happen after a non-modules 'export' declaration, as all other paths
check early for this case.

        PR c++/121832

gcc/cp/ChangeLog:

        * parser.cc (cp_parser_explicit_template_declaration): Check for
        null.

gcc/testsuite/ChangeLog:

        * g++.dg/concepts/pr121832.C: New test.

Signed-off-by: Nathaniel Shead <[email protected]>
---
 gcc/cp/parser.cc                         |  2 +-
 gcc/testsuite/g++.dg/concepts/pr121832.C | 10 ++++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/concepts/pr121832.C

diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index 392d1f90ac8..4d988c27cb8 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -36275,7 +36275,7 @@ cp_parser_explicit_template_declaration (cp_parser* 
parser, bool member_p)
   cp_parser_require_end_of_template_parameter_list (parser);
 
   /* Manage template requirements */
-  if (flag_concepts)
+  if (flag_concepts && current_template_parms)
   {
     tree reqs = get_shorthand_constraints (current_template_parms);
     if (tree treqs = cp_parser_requires_clause_opt (parser, false))
diff --git a/gcc/testsuite/g++.dg/concepts/pr121832.C 
b/gcc/testsuite/g++.dg/concepts/pr121832.C
new file mode 100644
index 00000000000..9a4c5c045d4
--- /dev/null
+++ b/gcc/testsuite/g++.dg/concepts/pr121832.C
@@ -0,0 +1,10 @@
+// { dg-do compile }
+// { dg-additional-options "-fconcepts -fno-modules" }
+
+export template<> a{};  // { dg-error "explicit spec" }
+// { dg-warning "'export' not implemented" "" { target c++98_only } .-1 }
+// { dg-warning "'export' is deprecated" "" { target { c++11 && c++17_down } } 
.-2 }
+// { dg-warning "'export' is enabled with '-fmodules'" "" { target c++20 } .-3 
}
+
+// { dg-prune-output "'a' does not name a type" }
+// { dg-prune-output "extra ';' outside of a function" }
-- 
2.51.0

Reply via email to