From 399e12c973ec12ee66b6038ad5acbdf574fd95b0 Mon Sep 17 00:00:00 2001 From: Cristian Adam Date: Fri, 6 Oct 2023 17:55:57 +0200 Subject: [PATCH] CMakePM: Fix crash in findLinkAt Fixes: QTCREATORBUG-29715 Change-Id: I4db919c8858631beb573789d1888b3dbee606c50 Reviewed-by: Christian Stenger --- .../cmakeprojectmanager/cmakeeditor.cpp | 53 ++++++++++--------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/src/plugins/cmakeprojectmanager/cmakeeditor.cpp b/src/plugins/cmakeprojectmanager/cmakeeditor.cpp index c06edffd322..1979d6ef76d 100644 --- a/src/plugins/cmakeprojectmanager/cmakeeditor.cpp +++ b/src/plugins/cmakeprojectmanager/cmakeeditor.cpp @@ -310,7 +310,7 @@ void CMakeEditorWidget::findLinkAt(const QTextCursor &cursor, if (auto project = ProjectTree::currentProject()) { buffer.replace("${CMAKE_SOURCE_DIR}", project->projectDirectory().path()); - if (auto bs = ProjectTree::currentBuildSystem()) { + if (auto bs = ProjectTree::currentBuildSystem(); bs->buildConfiguration()) { buffer.replace("${CMAKE_BINARY_DIR}", bs->buildConfiguration()->buildDirectory().path()); // Get the path suffix from current source dir to project source dir and apply it @@ -327,35 +327,36 @@ void CMakeEditorWidget::findLinkAt(const QTextCursor &cursor, .path()); // Check if the symbols is a user defined function or macro - const CMakeBuildSystem *cbs = static_cast(bs); - // Strip variable coating - if (buffer.startsWith("${") && buffer.endsWith("}")) - buffer = buffer.mid(2, buffer.size() - 3); + if (const auto cbs = qobject_cast(bs)) { + // Strip variable coating + if (buffer.startsWith("${") && buffer.endsWith("}")) + buffer = buffer.mid(2, buffer.size() - 3); - if (cbs->cmakeSymbolsHash().contains(buffer)) { - link = cbs->cmakeSymbolsHash().value(buffer); - addTextStartEndToLink(link); - return processLinkCallback(link); - } + if (cbs->cmakeSymbolsHash().contains(buffer)) { + link = cbs->cmakeSymbolsHash().value(buffer); + addTextStartEndToLink(link); + return processLinkCallback(link); + } - // Handle include(CMakeFileWithoutSuffix) and find_package(Package) - QString functionName; - if (funcStart > funcEnd) { - int funcStartPos = findWordStart(funcStart); - functionName = textDocument()->textAt(funcStartPos, funcStart - funcStartPos); + // Handle include(CMakeFileWithoutSuffix) and find_package(Package) + QString functionName; + if (funcStart > funcEnd) { + int funcStartPos = findWordStart(funcStart); + functionName = textDocument()->textAt(funcStartPos, funcStart - funcStartPos); - struct FunctionToHash - { - QString functionName; - const QHash &hash; - } functionToHashes[] = {{"include", cbs->dotCMakeFilesHash()}, - {"find_package", cbs->findPackagesFilesHash()}}; + struct FunctionToHash + { + QString functionName; + const QHash &hash; + } functionToHashes[] = {{"include", cbs->dotCMakeFilesHash()}, + {"find_package", cbs->findPackagesFilesHash()}}; - for (const auto &pair : functionToHashes) { - if (functionName == pair.functionName && pair.hash.contains(buffer)) { - link = pair.hash.value(buffer); - addTextStartEndToLink(link); - return processLinkCallback(link); + for (const auto &pair : functionToHashes) { + if (functionName == pair.functionName && pair.hash.contains(buffer)) { + link = pair.hash.value(buffer); + addTextStartEndToLink(link); + return processLinkCallback(link); + } } } }