[clangd] Fix a crash in semantic highlighting.
authorHaojian Wu <hokein.wu@gmail.com>
Tue, 7 Feb 2023 11:40:00 +0000 (12:40 +0100)
committerHaojian Wu <hokein.wu@gmail.com>
Tue, 7 Feb 2023 13:54:39 +0000 (14:54 +0100)
We encounter a few internal reports that we're dereference a nullptr.
Unfortunately, no small reproduce testcase for this crash yet, but it makes the
clangd more robost on broken code.

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

clang-tools-extra/clangd/SemanticHighlighting.cpp

index e3022ad..19f79f7 100644 (file)
@@ -519,10 +519,11 @@ private:
     Loc = getHighlightableSpellingToken(Loc, SourceMgr);
     if (Loc.isInvalid())
       return std::nullopt;
-
+    // We might have offsets in the main file that don't correspond to any
+    // spelled tokens.
     const auto *Tok = TB.spelledTokenAt(Loc);
-    assert(Tok);
-
+    if (!Tok)
+      return std::nullopt;
     return halfOpenToRange(SourceMgr,
                            Tok->range(SourceMgr).toCharRange(SourceMgr));
   }