From 20cb9b6e6927d02be151c0de7795732a3ad50f99 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Mon, 3 Jul 2023 13:03:21 +0200 Subject: [PATCH] ClangCodeModel: Do not call FilePath::exists() on random strings Fixes: QTCREATORBUG-29356 Change-Id: I6f4d89a6823829a7ecf16200786621321fea61a5 Reviewed-by: David Schulz Reviewed-by: --- src/plugins/clangcodemodel/clangdclient.cpp | 18 +++++++++++++++++- src/plugins/languageclient/client.cpp | 5 +++++ src/plugins/languageclient/client.h | 1 + 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/plugins/clangcodemodel/clangdclient.cpp b/src/plugins/clangcodemodel/clangdclient.cpp index 898f0ab535e..7c40086f09c 100644 --- a/src/plugins/clangcodemodel/clangdclient.cpp +++ b/src/plugins/clangcodemodel/clangdclient.cpp @@ -1114,7 +1114,23 @@ void ClangdClient::gatherHelpItemForTooltip(const HoverRequest::Response &hoverR cleanString.remove('`'); const QStringList lines = cleanString.trimmed().split('\n'); for (const QString &line : lines) { - const auto markupFilePath = Utils::FilePath::fromUserInput(line.simplified()); + const QString possibleFilePath = line.simplified(); + const auto looksLikeFilePath = [&] { + if (possibleFilePath.length() < 3) + return false; + if (osType() == OsTypeWindows) { + if (possibleFilePath.startsWith(R"(\\)")) + return true; + return possibleFilePath.front().isLetter() + && possibleFilePath.at(1) == ':' + && possibleFilePath.at(2) == '\\'; + } + return possibleFilePath.front() == '/' + && possibleFilePath.at(1).isLetterOrNumber(); + }; + if (!looksLikeFilePath()) + continue; + const auto markupFilePath = Utils::FilePath::fromUserInput(possibleFilePath); if (markupFilePath.exists()) { d->setHelpItemForTooltip(hoverResponse.id(), filePath, diff --git a/src/plugins/languageclient/client.cpp b/src/plugins/languageclient/client.cpp index 79e6b9a738c..da8a0cf19db 100644 --- a/src/plugins/languageclient/client.cpp +++ b/src/plugins/languageclient/client.cpp @@ -2169,6 +2169,11 @@ DocumentUri Client::hostPathToServerUri(const Utils::FilePath &path) const }); } +OsType Client::osType() const +{ + return d->m_serverDeviceTemplate.osType(); +} + void Client::registerCustomMethod(const QString &method, const CustomMethodHandler &handler) { d->m_customHandlers.insert(method, handler); diff --git a/src/plugins/languageclient/client.h b/src/plugins/languageclient/client.h index 9c934ea1611..006559760c5 100644 --- a/src/plugins/languageclient/client.h +++ b/src/plugins/languageclient/client.h @@ -168,6 +168,7 @@ public: LanguageServerProtocol::DocumentUri::PathMapper hostPathMapper() const; Utils::FilePath serverUriToHostPath(const LanguageServerProtocol::DocumentUri &uri) const; LanguageServerProtocol::DocumentUri hostPathToServerUri(const Utils::FilePath &path) const; + Utils::OsType osType() const; // custom methods using CustomMethodHandler = std::function