llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tools-extra

Author: Dhruv (dhr412)

<details>
<summary>Changes</summary>

Prevents a SIGSEGV crash when a client sends a URI that points to a directory 
instead of a file in `textDocument/didOpen` and `textDocument/documentSymbol` 
requests.

Closes #<!-- -->177466 

---
Full diff: https://github.com/llvm/llvm-project/pull/177834.diff


1 Files Affected:

- (modified) clang-tools-extra/clangd/ClangdLSPServer.cpp (+8) 


``````````diff
diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp 
b/clang-tools-extra/clangd/ClangdLSPServer.cpp
index 761b07eceec83..58b130d185a4e 100644
--- a/clang-tools-extra/clangd/ClangdLSPServer.cpp
+++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp
@@ -33,6 +33,7 @@
 #include "llvm/ADT/Twine.h"
 #include "llvm/Support/Allocator.h"
 #include "llvm/Support/Error.h"
+#include "llvm/Support/FileSystem.h"
 #include "llvm/Support/FormatVariadic.h"
 #include "llvm/Support/JSON.h"
 #include "llvm/Support/SHA1.h"
@@ -730,6 +731,9 @@ void ClangdLSPServer::onSync(const NoParams &, 
Callback<std::nullptr_t> Reply) {
 void ClangdLSPServer::onDocumentDidOpen(
     const DidOpenTextDocumentParams &Params) {
   PathRef File = Params.textDocument.uri.file();
+  if (llvm::sys::fs::is_directory(File)) {
+    return;
+  }
 
   const std::string &Contents = Params.textDocument.text;
 
@@ -1027,6 +1031,10 @@ flattenSymbolHierarchy(llvm::ArrayRef<DocumentSymbol> 
Symbols,
 void ClangdLSPServer::onDocumentSymbol(const DocumentSymbolParams &Params,
                                        Callback<llvm::json::Value> Reply) {
   URIForFile FileURI = Params.textDocument.uri;
+  if (llvm::sys::fs::is_directory(FileURI.file())) {
+    return Reply(llvm::make_error<LSPError>("URI is a directory",
+                                            ErrorCode::InvalidParams));
+  }
   Server->documentSymbols(
       Params.textDocument.uri.file(),
       [this, FileURI, Reply = std::move(Reply)](

``````````

</details>


https://github.com/llvm/llvm-project/pull/177834
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to