From b2abc6fa8e28e65142df52976c10b8a09b28f91d Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Tue, 11 Mar 2025 10:55:43 +0100 Subject: [PATCH] ProjectExplorer: Fix possible crash when renaming files An event loop might run during renameFiles(), so we cannot be sure that the nodes still exist in the end. Amends 05430afdcf1e10f4cc9b43407f996993a6175854. Task-number: QDS-14695 Change-Id: If93ea2bea525fc1d4a191e4feaa243399cba6957 Reviewed-by: Tim Jenssen --- src/plugins/projectexplorer/projectexplorer.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/plugins/projectexplorer/projectexplorer.cpp b/src/plugins/projectexplorer/projectexplorer.cpp index 4f265650797..aebc97270f4 100644 --- a/src/plugins/projectexplorer/projectexplorer.cpp +++ b/src/plugins/projectexplorer/projectexplorer.cpp @@ -2495,6 +2495,13 @@ FilePairs ProjectExplorerPlugin::renameFiles( = Utils::filtered(nodesAndNewFilePaths, [](const std::pair &elem) { return !elem.first->filePath().equalsCaseSensitive(elem.second); }); + + // The same as above, for use when the nodes might no longer exist. + const QList> oldAndNewFilePathsFiltered + = Utils::transform(nodesAndNewFilePathsFiltered, [](const std::pair &p) { + return std::make_pair(p.first->filePath(), p.second); + }); + FilePaths renamedOnly; FilePaths failedRenamings; const auto renameFile = [&failedRenamings](const Node *node, const FilePath &newFilePath) { @@ -2560,9 +2567,9 @@ FilePairs ProjectExplorerPlugin::renameFiles( } FilePairs allRenamedFiles; - for (const std::pair &candidate : nodesAndNewFilePathsFiltered) { - if (!failedRenamings.contains(candidate.first->filePath())) - allRenamedFiles.emplaceBack(candidate.first->filePath(), candidate.second); + for (const std::pair &candidate : oldAndNewFilePathsFiltered) { + if (!failedRenamings.contains(candidate.first)) + allRenamedFiles.emplaceBack(candidate.first, candidate.second); } emit instance()->filesRenamed(allRenamedFiles); return allRenamedFiles;