[clang-tools-extra] df0fd1c - [clangd] Use castAs<> instead of getAs<> to avoid dereference of nullptr

Simon Pilgrim via cfe-commits cfe-commits at lists.llvm.org
Sun Jan 23 05:35:04 PST 2022


Author: Simon Pilgrim
Date: 2022-01-23T13:24:36Z
New Revision: df0fd1c301d6a17c1cdeea1f19154e60a5b29f47

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

LOG: [clangd] Use castAs<> instead of getAs<> to avoid dereference of nullptr

The pointer is dereferenced immediately, so assert the cast is correct instead of returning nullptr

Added: 
    

Modified: 
    clang-tools-extra/clangd/HeuristicResolver.cpp
    clang-tools-extra/clangd/Hover.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/HeuristicResolver.cpp b/clang-tools-extra/clangd/HeuristicResolver.cpp
index 2505280ffa9aa..37e8f134efdfc 100644
--- a/clang-tools-extra/clangd/HeuristicResolver.cpp
+++ b/clang-tools-extra/clangd/HeuristicResolver.cpp
@@ -59,9 +59,8 @@ const Type *HeuristicResolver::getPointeeType(const Type *T) const {
   if (!T)
     return nullptr;
 
-  if (T->isPointerType()) {
-    return T->getAs<PointerType>()->getPointeeType().getTypePtrOrNull();
-  }
+  if (T->isPointerType())
+    return T->castAs<PointerType>()->getPointeeType().getTypePtrOrNull();
 
   // Try to handle smart pointer types.
 

diff  --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp
index 1449faec559cd..58ef2e3feb99d 100644
--- a/clang-tools-extra/clangd/Hover.cpp
+++ b/clang-tools-extra/clangd/Hover.cpp
@@ -147,7 +147,7 @@ HoverInfo::PrintedType printType(QualType QT, ASTContext &ASTCtx,
   // FIXME: This doesn't handle composite types that contain a decltype in them.
   // We should rather have a printing policy for that.
   while (!QT.isNull() && QT->isDecltypeType())
-    QT = QT->getAs<DecltypeType>()->getUnderlyingType();
+    QT = QT->castAs<DecltypeType>()->getUnderlyingType();
   HoverInfo::PrintedType Result;
   llvm::raw_string_ostream OS(Result.Type);
   // Special case: if the outer type is a tag type without qualifiers, then


        


More information about the cfe-commits mailing list