[sdk/kommit] dolphinplugins: dolphinplugins: free every repository StatusCache opens, once

Méven Car <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 3b80201fc56bd538d95932b8622428f6b6c1ef91 by Méven Car.
Committed on 28/07/2026 at 09:05.
Pushed by hamedmasafi into branch 'master'.

dolphinplugins: free every repository StatusCache opens, once

submodulePath() left behind the repository it opened, and the frees elsewhere
in the file ran on a pointer git_repository_open_ext never wrote to, or on one
that had been freed already.

M  +19   -16   dolphinplugins/statuscache.cpp

https://invent.kde.org/sdk/kommit/-/commit/3b80201fc56bd538d95932b8622428f6b6c1ef91

diff --git a/dolphinplugins/statuscache.cpp b/dolphinplugins/statuscache.cpp
index 77baade2..1854d66e 100644
--- a/dolphinplugins/statuscache.cpp
+++ b/dolphinplugins/statuscache.cpp
@@ -148,21 +148,21 @@ QStringList submodules(git_repository *repo)
 QString submodulePath(const QString &rootPath, const QString &childPath)
 {
     qDebug() << Q_FUNC_INFO << rootPath << childPath;
-    git_repository *repo;
-    auto n = git_repository_open_ext(&repo, rootPath.toUtf8().data(), GIT_REPOSITORY_OPEN_NO_SEARCH, NULL);
-
-    if (n) {
-        git_repository_free(repo);
+    git_repository *repo{nullptr};
+    if (git_repository_open_ext(&repo, rootPath.toUtf8().data(), GIT_REPOSITORY_OPEN_NO_SEARCH, NULL))
         return {};
-    }
 
-    auto submodulesList = submodules(repo);
+    QString name;
+    const auto submodulesList = submodules(repo);
     for (auto const &submodule : submodulesList) {
-        qDebug() << "FFFFF" << submodule << rootPath << childPath;
-        if (rootPath + QLatin1Char('/') + submodule + QLatin1Char('/') == childPath)
-            return submodule;
+        if (rootPath + QLatin1Char('/') + submodule + QLatin1Char('/') == childPath) {
+            name = submodule;
+            break;
+        }
     }
-    return {};
+
+    git_repository_free(repo);
+    return name;
 }
 }
 
@@ -191,13 +191,12 @@ bool StatusCache::setPath(const QString &path)
 
     mPath = path;
 
-    if (mRepo)
+    if (mRepo) {
         git_repository_free(mRepo);
+        mRepo = nullptr;
+    }
 
-    int n = git_repository_open_ext(&mRepo, path.toUtf8().data(), 0, NULL);
-
-    if (n) {
-        git_repository_free(mRepo);
+    if (git_repository_open_ext(&mRepo, path.toUtf8().data(), 0, NULL)) {
         mRepo = nullptr;
         return false;
     }
@@ -211,6 +210,7 @@ bool StatusCache::setPath(const QString &path)
         mCurrentPathIsIgnored = true;
 
         git_repository_free(mRepo);
+        mRepo = nullptr;
         return true;
     }
 
@@ -243,6 +243,9 @@ bool StatusCache::setPath(const QString &path)
 
 QString StatusCache::currentBranch() const
 {
+    if (!mRepo)
+        return {};
+
     if (git_repository_head_detached(mRepo) == 1)
         return {};
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.