]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: don't crash determining linkage of invalid TYPE_DECL [PR122391]
authorSimon Martin <simon@nasilyan.com>
Sun, 18 Jan 2026 10:07:10 +0000 (11:07 +0100)
committerSimon Martin <simon@nasilyan.com>
Sun, 18 Jan 2026 10:07:10 +0000 (11:07 +0100)
We currently ICE in decl_linkage for the following invalid input
because we access the TYPE_MAIN_VARIANT of error_mark_node

===
extern "C" {
  template <a> class b;
  struct {
    typedef b:
===

This patch fixes this by returning no linkage for any TYPE_DECL with an
erroneous type.

PR c++/122391

gcc/cp/ChangeLog:

* tree.cc (decl_linkage): Return lk_none for TYPE_DECLs with
erroneous type.

gcc/testsuite/ChangeLog:

* g++.dg/parse/bitfield10.C: New test.

gcc/cp/tree.cc
gcc/testsuite/g++.dg/parse/bitfield10.C [new file with mode: 0644]

index a57eefaf11f330ef43261d805ee792f50acd9b81..030af4d219d106b07662c663b2fb20ec66b25896 100644 (file)
@@ -6440,10 +6440,13 @@ decl_linkage (tree decl)
     {
       /* But this could be a typedef name for linkage purposes, in which
         case we're interested in the linkage of the main decl.  */
-      if (decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)))
-         /* Likewise for the injected-class-name.  */
-         || DECL_SELF_REFERENCE_P (decl))
-       decl = TYPE_MAIN_DECL (TREE_TYPE (decl));
+      tree type = TREE_TYPE (decl);
+      if (type == error_mark_node)
+       return lk_none;
+      else if (decl == TYPE_NAME (TYPE_MAIN_VARIANT (type))
+              /* Likewise for the injected-class-name.  */
+              || DECL_SELF_REFERENCE_P (decl))
+       decl = TYPE_MAIN_DECL (type);
       else
        return lk_none;
     }
diff --git a/gcc/testsuite/g++.dg/parse/bitfield10.C b/gcc/testsuite/g++.dg/parse/bitfield10.C
new file mode 100644 (file)
index 0000000..c5ab759
--- /dev/null
@@ -0,0 +1,9 @@
+/* PR c++/122391 */
+/* { dg-do "compile" } */
+
+extern "C" {
+  template <int> class b; /* { dg-error "with C linkage" } */
+  struct {
+    typedef b: /* { dg-error "at end of input" } */
+
+/* { dg-excess-errors "" } */