[llvm-branch-commits] [clang] 3bfae78 - Fix crash getting name of a template decl

Tom Stellard via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri May 20 04:03:30 PDT 2022


Author: Tom Eccles
Date: 2022-05-19T21:45:05-07:00
New Revision: 3bfae7816bdb5b09930f073f1eb99f72015d9f78

URL: https://github.com/llvm/llvm-project/commit/3bfae7816bdb5b09930f073f1eb99f72015d9f78
DIFF: https://github.com/llvm/llvm-project/commit/3bfae7816bdb5b09930f073f1eb99f72015d9f78.diff

LOG: 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 225b91e6cbba31ff1ce787a152a67977d08fdcab)

Added: 
    

Modified: 
    clang/lib/AST/TypePrinter.cpp
    clang/unittests/AST/TypePrinterTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/TypePrinter.cpp b/clang/lib/AST/TypePrinter.cpp
index bba323f651aad..6e827530f41b1 100644
--- a/clang/lib/AST/TypePrinter.cpp
+++ b/clang/lib/AST/TypePrinter.cpp
@@ -1466,8 +1466,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 8500d518d25fe..d60ecedea95fe 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 <template <typename ...> class TemplatedType>
+      void func(TemplatedType<int> Param);
+    )cpp";
+  auto Matcher = parmVarDecl(hasType(qualType().bind("id")));
+
+  // Regression test ensuring we do not segfault getting the QualType as a
+  // string.
+  ASSERT_TRUE(PrintedTypeMatches(Code, {}, Matcher, "<int>",
+                                 [](PrintingPolicy &Policy) {
+                                   Policy.FullyQualifiedName = true;
+                                   Policy.PrintCanonicalTypes = true;
+                                 }));
+}
+
 TEST(TypePrinter, ParamsUglified) {
   llvm::StringLiteral Code = R"cpp(
     template <typename _Tp, template <typename> class __f>


        


More information about the llvm-branch-commits mailing list