]> granicus.if.org Git - clang/commitdiff
[index] Return when DC is null in handleReference
authorFangrui Song <maskray@google.com>
Mon, 8 Jan 2018 18:57:38 +0000 (18:57 +0000)
committerFangrui Song <maskray@google.com>
Mon, 8 Jan 2018 18:57:38 +0000 (18:57 +0000)
Summary:
DC may sometimes be NULL and getContainerInfo(DC, Container) will dereference a null pointer.

Default template arguments (the following example and many test files in https://github.com/nlohmann/json)
may cause null pointer dereference.

```c++
template <typename>
struct actor;

template <template <typename> class Actor = actor>
struct terminal;
```

In tools/libclang/CXIndexDataConsumer.cpp#L203

    handleReference(ND, Loc, Cursor,
                    dyn_cast_or_null<NamedDecl>(ASTNode.Parent),
                    ASTNode.ContainerDC, ASTNode.OrigE, Kind);

`dyn_cast_or_null<NamedDecl>(ASTNode.Parent)` is somehow a null pointer and in tools/libclang/CXIndexDataConsumer.cpp:935

  ContainerInfo Container;
  getContainerInfo(DC, Container);

The null DC is casted `ContInfo.cursor = getCursor(cast<Decl>(DC));` and SIGSEGV.

```

See discussions in https://github.com/jacobdufault/cquery/issues/219 https://github.com/jacobdufault/cquery/issues/192

Reviewers: akyrtzi, sammccall, yvvan

Reviewed By: sammccall

Subscribers: mehdi_amini, cfe-commits

Differential Revision: https://reviews.llvm.org/D41575

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@322017 91177308-0d34-0410-b5e6-96231b3b80d8

tools/libclang/CXIndexDataConsumer.cpp

index 89ac23be734459205dc0bd32af74f2103d20ea1b..0db6f0c1f0788b0f1b90b6cdcca5c740458bcedb 100644 (file)
@@ -890,7 +890,7 @@ bool CXIndexDataConsumer::handleReference(const NamedDecl *D, SourceLocation Loc
                                       const DeclContext *DC,
                                       const Expr *E,
                                       CXIdxEntityRefKind Kind) {
-  if (!D)
+  if (!D || !DC)
     return false;
 
   CXCursor Cursor = E ? MakeCXCursor(E, cast<Decl>(DC), CXTU)
@@ -907,7 +907,7 @@ bool CXIndexDataConsumer::handleReference(const NamedDecl *D, SourceLocation Loc
   if (!CB.indexEntityReference)
     return false;
 
-  if (!D)
+  if (!D || !DC)
     return false;
   if (Loc.isInvalid())
     return false;