From: Simon Martin Date: Sun, 18 Jan 2026 10:07:10 +0000 (+0100) Subject: c++: don't crash determining linkage of invalid TYPE_DECL [PR122391] X-Git-Tag: basepoints/gcc-17~1921 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=e3bb887b3ee2baf118a67082c2bed2c8cc262875;p=thirdparty%2Fgcc.git c++: don't crash determining linkage of invalid TYPE_DECL [PR122391] 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 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. --- diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc index a57eefaf11f..030af4d219d 100644 --- a/gcc/cp/tree.cc +++ b/gcc/cp/tree.cc @@ -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 index 00000000000..c5ab75932e7 --- /dev/null +++ b/gcc/testsuite/g++.dg/parse/bitfield10.C @@ -0,0 +1,9 @@ +/* PR c++/122391 */ +/* { dg-do "compile" } */ + +extern "C" { + template class b; /* { dg-error "with C linkage" } */ + struct { + typedef b: /* { dg-error "at end of input" } */ + +/* { dg-excess-errors "" } */