From 225b91e6cbba31ff1ce787a152a67977d08fdcab Mon Sep 17 00:00:00 2001 From: Tom Eccles Date: Fri, 22 Apr 2022 13:03:28 -0400 Subject: [PATCH] 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. --- clang/lib/AST/TypePrinter.cpp | 3 +-- clang/unittests/AST/TypePrinterTest.cpp | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/clang/lib/AST/TypePrinter.cpp b/clang/lib/AST/TypePrinter.cpp index 32ca7643a760..d2feb1c10f06 100644 --- a/clang/lib/AST/TypePrinter.cpp +++ b/clang/lib/AST/TypePrinter.cpp @@ -1467,8 +1467,7 @@ void TypePrinter::printTemplateId(const TemplateSpecializationType *T, if (!Policy.SuppressScope) AppendScope(TD->getDeclContext(), OS, TD->getDeclName()); - IdentifierInfo *II = TD->getIdentifier(); - OS << II->getName(); + OS << TD->getName(); } else { T->getTemplateName().print(OS, Policy); } diff --git a/clang/unittests/AST/TypePrinterTest.cpp b/clang/unittests/AST/TypePrinterTest.cpp index 7b44b1ecece2..12801a701812 100644 --- a/clang/unittests/AST/TypePrinterTest.cpp +++ b/clang/unittests/AST/TypePrinterTest.cpp @@ -64,6 +64,22 @@ TEST(TypePrinter, TemplateId) { [](PrintingPolicy &Policy) { Policy.FullyQualifiedName = true; })); } +TEST(TypePrinter, TemplateId2) { + std::string Code = R"cpp( + template