[clang] 967ebad - Fix null ptr crash dumping TemplateTemplateParmDecl

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Sat Apr 24 09:28:24 PDT 2021


Author: David Tolnay
Date: 2021-04-24T12:28:10-04:00
New Revision: 967ebad12521f3187014a722804c27a75d44e649

URL: https://github.com/llvm/llvm-project/commit/967ebad12521f3187014a722804c27a75d44e649
DIFF: https://github.com/llvm/llvm-project/commit/967ebad12521f3187014a722804c27a75d44e649.diff

LOG: Fix null ptr crash dumping TemplateTemplateParmDecl

The following program winds up with
D->getDefaultArgStorage().getInheritedFrom() == nullptr
during dumping the TemplateTemplateParmDecl corresponding to the
template parameter of i.

  template <typename>
  struct R;
  template <template <typename> class = R>
  void i();

This patch fixes the null pointer dereference.

Added: 
    

Modified: 
    clang/lib/AST/JSONNodeDumper.cpp
    clang/test/AST/ast-dump-template-decls-json.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/JSONNodeDumper.cpp b/clang/lib/AST/JSONNodeDumper.cpp
index 7b99546bbe2d..5909812728a6 100644
--- a/clang/lib/AST/JSONNodeDumper.cpp
+++ b/clang/lib/AST/JSONNodeDumper.cpp
@@ -887,9 +887,10 @@ void JSONNodeDumper::VisitTemplateTemplateParmDecl(
 
   if (D->hasDefaultArgument())
     JOS.attributeObject("defaultArg", [=] {
+      const auto *InheritedFrom = D->getDefaultArgStorage().getInheritedFrom();
       Visit(D->getDefaultArgument().getArgument(),
-            D->getDefaultArgStorage().getInheritedFrom()->getSourceRange(),
-            D->getDefaultArgStorage().getInheritedFrom(),
+            InheritedFrom ? InheritedFrom->getSourceRange() : SourceLocation{},
+            InheritedFrom,
             D->defaultArgumentWasInherited() ? "inherited from" : "previous");
     });
 }

diff  --git a/clang/test/AST/ast-dump-template-decls-json.cpp b/clang/test/AST/ast-dump-template-decls-json.cpp
index f074d6f0137d..81b308e4fb74 100644
--- a/clang/test/AST/ast-dump-template-decls-json.cpp
+++ b/clang/test/AST/ast-dump-template-decls-json.cpp
@@ -55,6 +55,9 @@ template <typename Ty>
 template <typename Uy>
 void V<Ty>::f() {}
 
+template <template <typename> class = R>
+void i();
+
 // NOTE: CHECK lines have been autogenerated by gen_ast_dump_json_test.py
 
 
@@ -2578,6 +2581,127 @@ void V<Ty>::f() {}
 // CHECK-NEXT:      ]
 // CHECK-NEXT:     }
 // CHECK-NEXT:    ]
+// CHECK-NEXT:   },
+// CHECK-NEXT:   {
+// CHECK-NEXT:    "id": "0x{{.*}}",
+// CHECK-NEXT:    "kind": "FunctionTemplateDecl",
+// CHECK-NEXT:    "loc": {
+// CHECK-NEXT:     "offset": 922,
+// CHECK-NEXT:     "line": 59,
+// CHECK-NEXT:     "col": 6,
+// CHECK-NEXT:     "tokLen": 1
+// CHECK-NEXT:    },
+// CHECK-NEXT:    "range": {
+// CHECK-NEXT:     "begin": {
+// CHECK-NEXT:      "offset": 876,
+// CHECK-NEXT:      "line": 58,
+// CHECK-NEXT:      "col": 1,
+// CHECK-NEXT:      "tokLen": 8
+// CHECK-NEXT:     },
+// CHECK-NEXT:     "end": {
+// CHECK-NEXT:      "offset": 924,
+// CHECK-NEXT:      "line": 59,
+// CHECK-NEXT:      "col": 8,
+// CHECK-NEXT:      "tokLen": 1
+// CHECK-NEXT:     }
+// CHECK-NEXT:    },
+// CHECK-NEXT:    "name": "i",
+// CHECK-NEXT:    "inner": [
+// CHECK-NEXT:     {
+// CHECK-NEXT:      "id": "0x{{.*}}",
+// CHECK-NEXT:      "kind": "TemplateTemplateParmDecl",
+// CHECK-NEXT:      "loc": {
+// CHECK-NEXT:       "offset": 912,
+// CHECK-NEXT:       "line": 58,
+// CHECK-NEXT:       "col": 37,
+// CHECK-NEXT:       "tokLen": 1
+// CHECK-NEXT:      },
+// CHECK-NEXT:      "range": {
+// CHECK-NEXT:       "begin": {
+// CHECK-NEXT:        "offset": 886,
+// CHECK-NEXT:        "col": 11,
+// CHECK-NEXT:        "tokLen": 8
+// CHECK-NEXT:       },
+// CHECK-NEXT:       "end": {
+// CHECK-NEXT:        "offset": 914,
+// CHECK-NEXT:        "col": 39,
+// CHECK-NEXT:        "tokLen": 1
+// CHECK-NEXT:       }
+// CHECK-NEXT:      },
+// CHECK-NEXT:      "depth": 0,
+// CHECK-NEXT:      "index": 0,
+// CHECK-NEXT:      "defaultArg": {
+// CHECK-NEXT:       "kind": "TemplateArgument"
+// CHECK-NEXT:      },
+// CHECK-NEXT:      "inner": [
+// CHECK-NEXT:       {
+// CHECK-NEXT:        "id": "0x{{.*}}",
+// CHECK-NEXT:        "kind": "TemplateTypeParmDecl",
+// CHECK-NEXT:        "loc": {
+// CHECK-NEXT:         "offset": 904,
+// CHECK-NEXT:         "col": 29,
+// CHECK-NEXT:         "tokLen": 1
+// CHECK-NEXT:        },
+// CHECK-NEXT:        "range": {
+// CHECK-NEXT:         "begin": {
+// CHECK-NEXT:          "offset": 896,
+// CHECK-NEXT:          "col": 21,
+// CHECK-NEXT:          "tokLen": 8
+// CHECK-NEXT:         },
+// CHECK-NEXT:         "end": {
+// CHECK-NEXT:          "offset": 896,
+// CHECK-NEXT:          "col": 21,
+// CHECK-NEXT:          "tokLen": 8
+// CHECK-NEXT:         }
+// CHECK-NEXT:        },
+// CHECK-NEXT:        "tagUsed": "typename",
+// CHECK-NEXT:        "depth": 1,
+// CHECK-NEXT:        "index": 0
+// CHECK-NEXT:       },
+// CHECK-NEXT:       {
+// CHECK-NEXT:        "kind": "TemplateArgument",
+// CHECK-NEXT:        "range": {
+// CHECK-NEXT:         "begin": {
+// CHECK-NEXT:          "offset": 914,
+// CHECK-NEXT:          "col": 39,
+// CHECK-NEXT:          "tokLen": 1
+// CHECK-NEXT:         },
+// CHECK-NEXT:         "end": {
+// CHECK-NEXT:          "offset": 914,
+// CHECK-NEXT:          "col": 39,
+// CHECK-NEXT:          "tokLen": 1
+// CHECK-NEXT:         }
+// CHECK-NEXT:        }
+// CHECK-NEXT:       }
+// CHECK-NEXT:      ]
+// CHECK-NEXT:     },
+// CHECK-NEXT:     {
+// CHECK-NEXT:      "id": "0x{{.*}}",
+// CHECK-NEXT:      "kind": "FunctionDecl",
+// CHECK-NEXT:      "loc": {
+// CHECK-NEXT:       "offset": 922,
+// CHECK-NEXT:       "line": 59,
+// CHECK-NEXT:       "col": 6,
+// CHECK-NEXT:       "tokLen": 1
+// CHECK-NEXT:      },
+// CHECK-NEXT:      "range": {
+// CHECK-NEXT:       "begin": {
+// CHECK-NEXT:        "offset": 917,
+// CHECK-NEXT:        "col": 1,
+// CHECK-NEXT:        "tokLen": 4
+// CHECK-NEXT:       },
+// CHECK-NEXT:       "end": {
+// CHECK-NEXT:        "offset": 924,
+// CHECK-NEXT:        "col": 8,
+// CHECK-NEXT:        "tokLen": 1
+// CHECK-NEXT:       }
+// CHECK-NEXT:      },
+// CHECK-NEXT:      "name": "i",
+// CHECK-NEXT:      "type": {
+// CHECK-NEXT:       "qualType": "void ()"
+// CHECK-NEXT:      }
+// CHECK-NEXT:     }
+// CHECK-NEXT:    ]
 // CHECK-NEXT:   }
 // CHECK-NEXT:  ]
 // CHECK-NEXT: }


        


More information about the cfe-commits mailing list