llvm / llvm/llvm-project

[clangd] crashes with a SIGSEGV when receiving a textDocument/didOpen request where the URI points to a directory instead of a file.

Open
#177,466 8 comments 0 reactions 0 assignees View on GitHub
clangd crash good first issue
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

**Description:**
clangd crashes with a SIGSEGV when receiving a textDocument/didOpen or textDocument/documentSymbol request where the URI points to a directory instead of a file.

**Steps to reproduce:**
Start clangd in LSP mode.
Send the following JSON-RPC request:
code
JSON
{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"file:///","languageId":"c","version":1,"text":""}}}

**Expected result:**
The server should return an error response or ignore the request.

**Actual result:**
clangd crashes.

```
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
#0 0x000055676cd8e2f8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/home/mingo/local/clang-20/bin/clangd+0xa4f2f8)
#1 0x000055676cd8c1ee llvm::sys::RunSignalHandlers() (/home/mingo/local/clang-20/bin/clangd+0xa4d1ee)
#2 0x000055676cd8ec9d SignalHandler(int) Signals.cpp:0:0
#3 0x00007f831aff1980 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x12980)
#4 0x000055676d327160 bool llvm::DenseMapBase, llvm::detail::DenseMapPair>, clang::FileEntryRef, clang::SrcMgr::ContentCache*, llvm::DenseMapInfo, llvm::detail::DenseMapPair>::LookupBucketFor(clang::FileEntryRef const&, llvm::detail::DenseMapPair*&) (/home/mingo/local/clang-20/bin/clangd+0xfe8160)
#5 0x000055676d31ef77 llvm::DenseMapBase, llvm::detail::DenseMapPair>, clang::FileEntryRef, clang::SrcMgr::ContentCache*, llvm::DenseMapInfo, llvm::detail::DenseMapPair>::operator[](clang::FileEntryRef const&) (/home/mingo/local/clang-20/bin/clangd+0xfdff77)
#6 0x000055676d31efc7 clang::SourceManager::getOrCreateContentCache(clang::FileEntryRef, bool) (/home/mingo/local/clang-20/bin/clangd+0xfdffc7)
#7 0x000055676d326799 clang::SourceManagerForFile::SourceManagerForFile(llvm::StringRef, llvm::StringRef) (/home/mingo/local/clang-20/bin/clangd+0xfe7799)
#8 0x000055677057ebec clang::tooling::(anonymous namespace)::getOffsetAfterTokenSequence(llvm::StringRef, llvm::StringRef, clang::tooling::IncludeStyle const&, llvm::function_ref) HeaderIncludes.cpp:0:0
#9 0x000055677057d867 clang::tooling::HeaderIncludes::HeaderIncludes(llvm::StringRef, llvm::StringRef, clang::tooling::IncludeStyle const&) (/home/mingo/local/clang-20/bin/clangd+0x423e867)
#10 0x000055676dd96cc7 clang::clangd::ParsedAST::build(llvm::StringRef, clang::clangd::ParseInputs const&, std::unique_ptr>, llvm::ArrayRef, std::shared_ptr) (/home/mingo/local/clang-20/bin/clangd+0x1a57cc7)
#11 0x000055676de33982 clang::clangd::(anonymous namespace)::ASTWorker::generateDiagnostics(std::unique_ptr>, clang::clangd::ParseInputs, std::vector>) TUScheduler.cpp:0:0
#12 0x000055676de333fa clang::clangd::(anonymous namespace)::ASTWorker::updatePreamble(std::unique_ptr>, clang::clangd::ParseInputs, std::shared_ptr, std::vector>, clang::clangd::WantDiagnostics)::$_0::operator()() TUScheduler.cpp:0:0
#13 0x000055676de30049 clang::clangd::(anonymous namespace)::ASTWorker::runTask(llvm::StringRef, llvm::function_ref) TUScheduler.cpp:0:0
#14 0x000055676de2fb2a void llvm::detail::UniqueFunctionBase::CallImpl(void*) TUScheduler.cpp:0:0
#15 0x000055676df9c2c2 void* llvm::thread::ThreadProxy)::$_1>>(void*) Threading.cpp:0:0
#16 0x00007f831afe66db start_thread /build/glibc-CVJwZb/glibc-2.27/nptl/pthread_create.c:463:0
#17 0x00007f8319cc561f __clone /build/glibc-CVJwZb/glibc-2.27/misc/../sysdeps/unix/sysv/linux/x86_64/clone.S:97:0
Signalled during AST worker action: Build AST
Filename: /
Directory:
Command Line: /home/mingo/local/clang-20/bin/clang -xobjective-c++-header -resource-dir=/home/mingo/local/clang-20/lib/clang/20 -- /
Version: 1
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the directory-URI request in clangd LSP mode, then trace the stack from ASTWorker::generateDiagnostics in TUScheduler.cpp through ParsedAST::build and HeaderIncludes.cpp. Done means textDocument/didOpen and textDocument/documentSymbol requests for a directory return an error or are ignored without a SIGSEGV.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.