From aca5de93b03995ae03f293f5caa3ccc2e2016afb Mon Sep 17 00:00:00 2001
From: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
Date: Thu, 17 Jun 2021 16:54:54 -0400
Subject: [PATCH] conversations: prevent segfault when git repo can't be opened

Change-Id: Iff94a21f26fd1518e1d7287e511c41176ea8eea9
---
 src/jamidht/conversationrepository.cpp | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/src/jamidht/conversationrepository.cpp b/src/jamidht/conversationrepository.cpp
index 79f9c2824a..8d2f37ad1e 100644
--- a/src/jamidht/conversationrepository.cpp
+++ b/src/jamidht/conversationrepository.cpp
@@ -62,8 +62,13 @@ public:
         auto path = fileutils::get_data_dir() + "/" + shared->getAccountID() + "/" + "conversations"
                     + "/" + id_;
         git_repository* repo = nullptr;
-        if (git_repository_open(&repo, path.c_str()) != 0)
+        auto err = git_repository_open(&repo, path.c_str());
+        if (err < 0) {
+            JAMI_ERR("Couldn't open git repository: %s (%s)",
+                     path.c_str(),
+                     git_error_last()->message);
             return {nullptr, git_repository_free};
+        }
         return {std::move(repo), git_repository_free};
     }
 
@@ -2631,11 +2636,11 @@ void
 ConversationRepository::erase()
 {
     // First, we need to add the member file to the repository if not present
-    auto repo = pimpl_->repository();
-    std::string repoPath = git_repository_workdir(repo.get());
-
-    JAMI_DBG() << "Erasing " << repoPath;
-    fileutils::removeAll(repoPath, true);
+    if (auto repo = pimpl_->repository()) {
+        std::string repoPath = git_repository_workdir(repo.get());
+        JAMI_DBG() << "Erasing " << repoPath;
+        fileutils::removeAll(repoPath, true);
+    }
 }
 
 ConversationMode
-- 
GitLab