[clang-tools-extra] 41a6085 - [clangd] Fix a crash in semantic highlighting.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 7 05:55:30 PST 2023


Author: Haojian Wu
Date: 2023-02-07T14:54:39+01:00
New Revision: 41a6085252efe4c2b806e03d923c0b0b35fc3baa

URL: https://github.com/llvm/llvm-project/commit/41a6085252efe4c2b806e03d923c0b0b35fc3baa
DIFF: https://github.com/llvm/llvm-project/commit/41a6085252efe4c2b806e03d923c0b0b35fc3baa.diff

LOG: [clangd] Fix a crash in semantic highlighting.

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

Added: 
    

Modified: 
    clang-tools-extra/clangd/SemanticHighlighting.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/SemanticHighlighting.cpp b/clang-tools-extra/clangd/SemanticHighlighting.cpp
index e3022ad131ff2..19f79f793420d 100644
--- a/clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ b/clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -519,10 +519,11 @@ class HighlightingsBuilder {
     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));
   }


        


More information about the cfe-commits mailing list