From 066624d59a765563a997268d560dfd67ac35b1be Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Wed, 21 Dec 2022 10:17:51 -0500
Subject: [PATCH] conversationrepository: fix "too big to fetch" error

stats->received_bytes contains the received size from the beginning
not per pack object. So, the previous check was incorrectly done
as it was checking an incorrect size.

GitLab: #768
Change-Id: I83dcda28298bcfc4c9c1c53f8639fa290f241307
---
 src/jamidht/conversationrepository.cpp | 30 ++++++++++++++------------
 1 file changed, 16 insertions(+), 14 deletions(-)

diff --git a/src/jamidht/conversationrepository.cpp b/src/jamidht/conversationrepository.cpp
index a10bc7a7dc..6f257a85c6 100644
--- a/src/jamidht/conversationrepository.cpp
+++ b/src/jamidht/conversationrepository.cpp
@@ -2500,14 +2500,15 @@ ConversationRepository::cloneConversation(const std::weak_ptr<JamiAccount>& acco
     git_clone_options clone_options;
     git_clone_options_init(&clone_options, GIT_CLONE_OPTIONS_VERSION);
     git_fetch_options_init(&clone_options.fetch_opts, GIT_FETCH_OPTIONS_VERSION);
-    size_t received_bytes = 0;
-    clone_options.fetch_opts.callbacks.payload = static_cast<void*>(&received_bytes);
     clone_options.fetch_opts.callbacks.transfer_progress = [](const git_indexer_progress* stats,
-                                                              void* payload) {
-        *(static_cast<size_t*>(payload)) += stats->received_bytes;
-        if (*(static_cast<size_t*>(payload)) > MAX_FETCH_SIZE) {
-            JAMI_ERR("Abort fetching repository, the fetch is too big: %lu bytes",
-                     *(static_cast<size_t*>(payload)));
+                                                              void*) {
+        // Uncomment to get advancment
+        // if (stats->received_objects % 500 == 0 || stats->received_objects == stats->total_objects)
+        //     JAMI_DEBUG("{}/{} {}kb", stats->received_objects, stats->total_objects, stats->received_bytes/1024);
+        // If a pack is more than 256Mb, it's anormal.
+        if (stats->received_bytes > MAX_FETCH_SIZE) {
+            JAMI_ERROR("Abort fetching repository, the fetch is too big: {} bytes ({}/{})",
+                stats->received_bytes, stats->received_objects, stats->total_objects);
             return -1;
         }
         return 0;
@@ -2897,13 +2898,14 @@ ConversationRepository::fetch(const std::string& remoteDeviceId)
     }
     GitRemote remote {remote_ptr, git_remote_free};
 
-    size_t received_bytes = 0;
-    fetch_opts.callbacks.payload = static_cast<void*>(&received_bytes);
-    fetch_opts.callbacks.transfer_progress = [](const git_indexer_progress* stats, void* payload) {
-        *(static_cast<size_t*>(payload)) += stats->received_bytes;
-        if (*(static_cast<size_t*>(payload)) > MAX_FETCH_SIZE) {
-            JAMI_ERR("Abort fetching repository, the fetch is too big: %lu bytes",
-                     *(static_cast<size_t*>(payload)));
+    fetch_opts.callbacks.transfer_progress = [](const git_indexer_progress* stats, void*) {
+        // Uncomment to get advancment
+        // if (stats->received_objects % 500 == 0 || stats->received_objects == stats->total_objects)
+        //     JAMI_DEBUG("{}/{} {}kb", stats->received_objects, stats->total_objects, stats->received_bytes/1024);
+        // If a pack is more than 256Mb, it's anormal.
+        if (stats->received_bytes > MAX_FETCH_SIZE) {
+            JAMI_ERROR("Abort fetching repository, the fetch is too big: {} bytes ({}/{})",
+                stats->received_bytes, stats->received_objects, stats->total_objects);
             return -1;
         }
         return 0;
-- 
GitLab