From cc6da747ac154ae9495bc2fa0e12726ecae73f8f Mon Sep 17 00:00:00 2001
From: Julien Wadel <julien.wadel@belledonne-communications.com>
Date: Mon, 3 Jul 2023 10:39:47 +0200
Subject: [PATCH] Fix a leak on repeating file downloading request. Add a test
 on cancelling a download request and restart it by keeping the file that was
 cancelled.

---
 .../modifier/file-transfer-chat-message-modifier.cpp  | 10 ++++++----
 tester/group_chat_tester.c                            | 11 +++++++++++
 2 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/src/chat/modifier/file-transfer-chat-message-modifier.cpp b/src/chat/modifier/file-transfer-chat-message-modifier.cpp
index 26d7d95639..022ce3042d 100644
--- a/src/chat/modifier/file-transfer-chat-message-modifier.cpp
+++ b/src/chat/modifier/file-transfer-chat-message-modifier.cpp
@@ -279,7 +279,7 @@ FileTransferChatMessageModifier::prepare_upload_body_handler(shared_ptr<ChatMess
 		belle_sip_user_body_handler_t *body_handler = (belle_sip_user_body_handler_t *)first_part_bh;
 		// No need to add again the callback for progression, otherwise it will be called twice
 		first_part_bh = (belle_sip_body_handler_t *)belle_sip_file_body_handler_new(
-		    currentFileContentToTransfer->getFilePathSys().c_str(), nullptr, this);
+		    currentFileContentToTransfer->getFilePathSys().c_str(), nullptr, this, BELLE_SIP_BODY_HANDLER_SEND);
 		belle_sip_file_body_handler_set_user_body_handler((belle_sip_file_body_handler_t *)first_part_bh, body_handler);
 		// Ensure the file size has been set to the correct value
 		currentFileTransferContent->setFileSize(
@@ -837,7 +837,7 @@ void FileTransferChatMessageModifier::processResponseHeadersFromGetFile(const be
 
 			body_handler = (belle_sip_body_handler_t *)belle_sip_buffering_file_body_handler_new(
 			    currentFileContentToTransfer->getFilePathSys().c_str(), 16, _chat_message_file_transfer_on_progress,
-			    this);
+			    this, BELLE_SIP_BODY_HANDLER_RECV);
 			if (belle_sip_body_handler_get_size((belle_sip_body_handler_t *)body_handler) == 0) {
 				// If the size of the body has not been initialized from the file stat, use the one from the
 				// file_transfer_information.
@@ -943,9 +943,11 @@ bool FileTransferChatMessageModifier::downloadFile(const shared_ptr<ChatMessage>
 		lError() << "Content type is not a FileTransfer.";
 		return false;
 	}
-
-	createFileContentFromFileTransferContent(fileTransferContent);
 	FileContent *fileContent = fileTransferContent->getFileContent();
+	if(fileContent)
+		delete fileContent;
+	createFileContentFromFileTransferContent(fileTransferContent);
+	fileContent = fileTransferContent->getFileContent();
 	currentFileContentToTransfer = fileContent;
 	if (!currentFileContentToTransfer) return false;
 	currentFileTransferContent = fileTransferContent;
diff --git a/tester/group_chat_tester.c b/tester/group_chat_tester.c
index dd70f14397..ef180360af 100644
--- a/tester/group_chat_tester.c
+++ b/tester/group_chat_tester.c
@@ -506,6 +506,17 @@ void _receive_file_plus_text(bctbx_list_t *coresList,
 		linphone_chat_message_download_content(msg, fileTransferContent);
 		BC_ASSERT_EQUAL(linphone_chat_message_get_state(msg), LinphoneChatMessageStateFileTransferInProgress, int,
 		                "%d");
+// Cancel the download
+		wait_for_list(coresList, NULL, 0, 400);
+
+		char * downloaded_file_temp = bctbx_concat(downloaded_file, ".copy", NULL);
+		remove(downloaded_file_temp);
+		liblinphone_tester_copy_file(downloaded_file, downloaded_file_temp);
+		linphone_chat_message_cancel_file_transfer(msg);
+		liblinphone_tester_copy_file(downloaded_file_temp, downloaded_file);
+		remove(downloaded_file_temp);
+		bctbx_free(downloaded_file_temp);
+		linphone_chat_message_download_content(msg, fileTransferContent);
 
 		if (BC_ASSERT_TRUE(wait_for_list(coresList, &lcm->stat.number_of_LinphoneMessageFileTransferDone,
 		                                 receiverStats->number_of_LinphoneMessageFileTransferDone + 1,
-- 
GitLab