From 79becbaeac933d49d30fe3a92cff949851337b38 Mon Sep 17 00:00:00 2001 From: Christian Stenger Date: Fri, 27 Oct 2023 14:46:31 +0200 Subject: [PATCH] CMakePM: Protect against accidental nullptr access Wizard summary page does not provide a list to put in failed files, so ending up inside the build systems addFiles() may pass in a nullptr. Guard against its access. Change-Id: Ia858f8029b1b840cc341d87614576f2634b995dc Reviewed-by: Alessandro Portale --- .../cmakeprojectmanager/cmakebuildsystem.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/plugins/cmakeprojectmanager/cmakebuildsystem.cpp b/src/plugins/cmakeprojectmanager/cmakebuildsystem.cpp index 7c0001c6a86..b5630faf28c 100644 --- a/src/plugins/cmakeprojectmanager/cmakebuildsystem.cpp +++ b/src/plugins/cmakeprojectmanager/cmakebuildsystem.cpp @@ -272,6 +272,8 @@ static QString newFilesForFunction(const std::string &cmakeFunction, bool CMakeBuildSystem::addFiles(Node *context, const FilePaths &filePaths, FilePaths *notAdded) { + if (notAdded) + *notAdded = filePaths; if (auto n = dynamic_cast(context)) { const QString targetName = n->buildKey(); auto target = Utils::findOrDefault(buildTargets(), @@ -279,10 +281,9 @@ bool CMakeBuildSystem::addFiles(Node *context, const FilePaths &filePaths, FileP return target.title == targetName; }); - if (target.backtrace.isEmpty()) { - *notAdded = filePaths; + if (target.backtrace.isEmpty()) return false; - } + const FilePath targetCMakeFile = target.backtrace.last().path; const int targetDefinitionLine = target.backtrace.last().line; @@ -297,7 +298,6 @@ bool CMakeBuildSystem::addFiles(Node *context, const FilePaths &filePaths, FileP if (!cmakeListFile.ParseString(fileContent->toStdString(), targetCMakeFile.fileName().toStdString(), errorString)) { - *notAdded = filePaths; return false; } } @@ -308,10 +308,8 @@ bool CMakeBuildSystem::addFiles(Node *context, const FilePaths &filePaths, FileP return func.Line() == targetDefinitionLine; }); - if (function == cmakeListFile.Functions.end()) { - *notAdded = filePaths; + if (function == cmakeListFile.Functions.end()) return false; - } // Special case: when qt_add_executable and qt_add_qml_module use the same target name // then qt_add_qml_module function should be used @@ -385,16 +383,16 @@ bool CMakeBuildSystem::addFiles(Node *context, const FilePaths &filePaths, FileP Core::EditorManager::openEditorAt({targetCMakeFile, line, column + extraChars}, Constants::CMAKE_EDITOR_ID, Core::EditorManager::DoNotMakeVisible)); - if (!editor) { - *notAdded = filePaths; + if (!editor) return false; - } editor->insert(snippet); editor->editorWidget()->autoIndent(); if (!Core::DocumentManager::saveDocument(editor->document())) return false; + if (notAdded) + notAdded->clear(); return true; }