Skip to content

Commit 3bfae78

Browse files
tblahtstellar
authored andcommitted
Fix crash getting name of a template decl
NamedDecl::getIdentifier can return a nullptr when DeclarationName::isIdentifier is false, which leads to a null pointer dereference when TypePrinter::printTemplateId calls ->getName(). NamedDecl::getName does the same thing in the successful case and returns an empty string in the failure case. This crash affects the llvm 14 packages on llvm.org. (cherry picked from commit 225b91e)
1 parent 76c1c1d commit 3bfae78

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

clang/lib/AST/TypePrinter.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,8 +1466,7 @@ void TypePrinter::printTemplateId(const TemplateSpecializationType *T,
14661466
if (!Policy.SuppressScope)
14671467
AppendScope(TD->getDeclContext(), OS, TD->getDeclName());
14681468

1469-
IdentifierInfo *II = TD->getIdentifier();
1470-
OS << II->getName();
1469+
OS << TD->getName();
14711470
} else {
14721471
T->getTemplateName().print(OS, Policy);
14731472
}

clang/unittests/AST/TypePrinterTest.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ TEST(TypePrinter, TemplateId) {
6464
[](PrintingPolicy &Policy) { Policy.FullyQualifiedName = true; }));
6565
}
6666

67+
TEST(TypePrinter, TemplateId2) {
68+
std::string Code = R"cpp(
69+
template <template <typename ...> class TemplatedType>
70+
void func(TemplatedType<int> Param);
71+
)cpp";
72+
auto Matcher = parmVarDecl(hasType(qualType().bind("id")));
73+
74+
// Regression test ensuring we do not segfault getting the QualType as a
75+
// string.
76+
ASSERT_TRUE(PrintedTypeMatches(Code, {}, Matcher, "<int>",
77+
[](PrintingPolicy &Policy) {
78+
Policy.FullyQualifiedName = true;
79+
Policy.PrintCanonicalTypes = true;
80+
}));
81+
}
82+
6783
TEST(TypePrinter, ParamsUglified) {
6884
llvm::StringLiteral Code = R"cpp(
6985
template <typename _Tp, template <typename> class __f>

0 commit comments

Comments
 (0)