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 <alessandro.portale@qt.io>
This commit is contained in:
Christian Stenger
2023-10-27 14:46:31 +02:00
parent 7891a7f5b4
commit 79becbaeac

View File

@@ -272,6 +272,8 @@ static QString newFilesForFunction(const std::string &cmakeFunction,
bool CMakeBuildSystem::addFiles(Node *context, const FilePaths &filePaths, FilePaths *notAdded) bool CMakeBuildSystem::addFiles(Node *context, const FilePaths &filePaths, FilePaths *notAdded)
{ {
if (notAdded)
*notAdded = filePaths;
if (auto n = dynamic_cast<CMakeTargetNode *>(context)) { if (auto n = dynamic_cast<CMakeTargetNode *>(context)) {
const QString targetName = n->buildKey(); const QString targetName = n->buildKey();
auto target = Utils::findOrDefault(buildTargets(), auto target = Utils::findOrDefault(buildTargets(),
@@ -279,10 +281,9 @@ bool CMakeBuildSystem::addFiles(Node *context, const FilePaths &filePaths, FileP
return target.title == targetName; return target.title == targetName;
}); });
if (target.backtrace.isEmpty()) { if (target.backtrace.isEmpty())
*notAdded = filePaths;
return false; return false;
}
const FilePath targetCMakeFile = target.backtrace.last().path; const FilePath targetCMakeFile = target.backtrace.last().path;
const int targetDefinitionLine = target.backtrace.last().line; 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(), if (!cmakeListFile.ParseString(fileContent->toStdString(),
targetCMakeFile.fileName().toStdString(), targetCMakeFile.fileName().toStdString(),
errorString)) { errorString)) {
*notAdded = filePaths;
return false; return false;
} }
} }
@@ -308,10 +308,8 @@ bool CMakeBuildSystem::addFiles(Node *context, const FilePaths &filePaths, FileP
return func.Line() == targetDefinitionLine; return func.Line() == targetDefinitionLine;
}); });
if (function == cmakeListFile.Functions.end()) { if (function == cmakeListFile.Functions.end())
*notAdded = filePaths;
return false; return false;
}
// Special case: when qt_add_executable and qt_add_qml_module use the same target name // 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 // 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}, Core::EditorManager::openEditorAt({targetCMakeFile, line, column + extraChars},
Constants::CMAKE_EDITOR_ID, Constants::CMAKE_EDITOR_ID,
Core::EditorManager::DoNotMakeVisible)); Core::EditorManager::DoNotMakeVisible));
if (!editor) { if (!editor)
*notAdded = filePaths;
return false; return false;
}
editor->insert(snippet); editor->insert(snippet);
editor->editorWidget()->autoIndent(); editor->editorWidget()->autoIndent();
if (!Core::DocumentManager::saveDocument(editor->document())) if (!Core::DocumentManager::saveDocument(editor->document()))
return false; return false;
if (notAdded)
notAdded->clear();
return true; return true;
} }