https://gcc.gnu.org/g:ff7d7898bb40d4bc72ad157d48eaa42fbe022512

commit r16-7220-gff7d7898bb40d4bc72ad157d48eaa42fbe022512
Author: Nathaniel Shead <[email protected]>
Date:   Fri Sep 12 23:26:20 2025 +1000

    c++: Fix error recovery after export keyword [PR121832]
    
    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]>

Diff:
---
 gcc/cp/parser.cc                         |  2 +-
 gcc/testsuite/g++.dg/concepts/pr121832.C | 10 ++++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index 392d1f90ac81..4d988c27cb80 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 000000000000..9a4c5c045d4e
--- /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" }

Reply via email to