From 40ada5153af6a71e6f23590bdb9375120c86ba09 Mon Sep 17 00:00:00 2001 From: Ghislain MARY Date: Tue, 30 Jan 2018 11:53:35 +0100 Subject: [PATCH] Fix some memory leaks and crashes in message tester. --- coreapi/CMakeLists.txt | 1 - coreapi/chat_file_transfer.c | 30 -------------------- coreapi/lime.c | 54 +++++++++++++++++++++++++----------- tester/message_tester.c | 11 ++++---- 4 files changed, 43 insertions(+), 53 deletions(-) delete mode 100644 coreapi/chat_file_transfer.c diff --git a/coreapi/CMakeLists.txt b/coreapi/CMakeLists.txt index ea1c499c3..03600de80 100644 --- a/coreapi/CMakeLists.txt +++ b/coreapi/CMakeLists.txt @@ -63,7 +63,6 @@ set(LINPHONE_SOURCE_FILES_C call_log.c carddav.c chat.c - chat_file_transfer.c contactprovider.c content.c dial_plan.c diff --git a/coreapi/chat_file_transfer.c b/coreapi/chat_file_transfer.c deleted file mode 100644 index 210551f45..000000000 --- a/coreapi/chat_file_transfer.c +++ /dev/null @@ -1,30 +0,0 @@ -/*************************************************************************** - * chat_file_transfer.c - * - * Sun Jun 5 19:34:18 2005 - * Copyright 2005 Simon Morlat - * Email simon dot morlat at linphone dot org - ****************************************************************************/ - -/* - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - */ - -#include "linphone/core.h" -#include "private.h" - -#include "c-wrapper/c-wrapper.h" -#include "chat/chat-room/chat-room.h" - diff --git a/coreapi/lime.c b/coreapi/lime.c index 9deb4cbdf..ac081a452 100644 --- a/coreapi/lime.c +++ b/coreapi/lime.c @@ -886,34 +886,52 @@ int lime_im_encryption_engine_process_outgoing_message_cb(LinphoneImEncryptionEn } int lime_im_encryption_engine_process_downloading_file_cb(LinphoneImEncryptionEngine *engine, LinphoneChatMessage *msg, size_t offset, const uint8_t *buffer, size_t size, uint8_t *decrypted_buffer) { - if (linphone_content_get_key(linphone_chat_message_get_file_transfer_information(msg)) == NULL) return -1; - - if (buffer == NULL || size == 0) { - return lime_decryptFile(linphone_content_get_cryptoContext_address(linphone_chat_message_get_file_transfer_information(msg)), NULL, 0, NULL, NULL); + LinphoneContent *content = linphone_chat_message_get_file_transfer_information(msg); + if (!content) + return -1; + if (!linphone_content_get_key(content)) { + linphone_content_unref(content); + return -1; } - return lime_decryptFile(linphone_content_get_cryptoContext_address(linphone_chat_message_get_file_transfer_information(msg)), - (unsigned char *)linphone_content_get_key(linphone_chat_message_get_file_transfer_information(msg)), size, (char *)decrypted_buffer, - (char *)buffer); + if (!buffer || (size == 0)) { + int result = lime_decryptFile(linphone_content_get_cryptoContext_address(content), NULL, 0, NULL, NULL); + linphone_content_unref(content); + return result; + } + + int result = lime_decryptFile(linphone_content_get_cryptoContext_address(content), + (unsigned char *)linphone_content_get_key(content), size, (char *)decrypted_buffer, (char *)buffer); + linphone_content_unref(content); + return result; } int lime_im_encryption_engine_process_uploading_file_cb(LinphoneImEncryptionEngine *engine, LinphoneChatMessage *msg, size_t offset, const uint8_t *buffer, size_t *size, uint8_t *encrypted_buffer) { - size_t file_size = linphone_content_get_size(linphone_chat_message_get_file_transfer_information(msg)); - if (linphone_content_get_key(linphone_chat_message_get_file_transfer_information(msg)) == NULL) return -1; - - if (buffer == NULL || *size == 0) { - return lime_encryptFile(linphone_content_get_cryptoContext_address(linphone_chat_message_get_file_transfer_information(msg)), NULL, 0, NULL, NULL); + LinphoneContent *content = linphone_chat_message_get_file_transfer_information(msg); + if (!content) + return -1; + if (!linphone_content_get_key(content)) { + linphone_content_unref(content); + return -1; } + if (!buffer || (*size == 0)) { + int result = lime_encryptFile(linphone_content_get_cryptoContext_address(content), NULL, 0, NULL, NULL); + linphone_content_unref(content); + return result; + } + + size_t file_size = linphone_content_get_size(content); if (file_size == 0) { ms_warning("File size has not been set, encryption will fail if not done in one step (if file is larger than 16K)"); } else if (offset + *size < file_size) { *size -= (*size % 16); } - return lime_encryptFile(linphone_content_get_cryptoContext_address(linphone_chat_message_get_file_transfer_information(msg)), - (unsigned char *)linphone_content_get_key(linphone_chat_message_get_file_transfer_information(msg)), *size, - (char *)buffer, (char *)encrypted_buffer); + int result = lime_encryptFile(linphone_content_get_cryptoContext_address(content), + (unsigned char *)linphone_content_get_key(content), *size, (char *)buffer, (char *)encrypted_buffer); + linphone_content_unref(content); + return result; } bool_t lime_im_encryption_engine_is_file_encryption_enabled_cb(LinphoneImEncryptionEngine *engine, LinphoneChatRoom *room) { @@ -926,7 +944,11 @@ void lime_im_encryption_engine_generate_file_transfer_key_cb(LinphoneImEncryptio /* generate a random 192 bits key + 64 bits of initial vector and store it into the * file_transfer_information->key field of the msg */ sal_get_random_bytes((unsigned char *)keyBuffer, FILE_TRANSFER_KEY_SIZE); - linphone_content_set_key(linphone_chat_message_get_file_transfer_information(msg), keyBuffer, FILE_TRANSFER_KEY_SIZE); /* key is duplicated in the content private structure */ + LinphoneContent *content = linphone_chat_message_get_file_transfer_information(msg); + if (!content) + return; + linphone_content_set_key(content, keyBuffer, FILE_TRANSFER_KEY_SIZE); /* key is duplicated in the content private structure */ + linphone_content_unref(content); } #else /* HAVE_LIME */ diff --git a/tester/message_tester.c b/tester/message_tester.c index 818a20bec..5082b37a8 100644 --- a/tester/message_tester.c +++ b/tester/message_tester.c @@ -68,8 +68,10 @@ void message_received(LinphoneCore *lc, LinphoneChatRoom *room, LinphoneChatMess linphone_chat_message_unref(counters->last_received_chat_message); } counters->last_received_chat_message=linphone_chat_message_ref(msg); - if (linphone_chat_message_get_file_transfer_information(msg)) { + LinphoneContent * content = linphone_chat_message_get_file_transfer_information(msg); + if (content) { counters->number_of_LinphoneMessageReceivedWithFile++; + linphone_content_unref(content); } else if (linphone_chat_message_get_external_body_url(msg)) { counters->number_of_LinphoneMessageExtBodyReceived++; if (message_external_body_url) { @@ -538,10 +540,6 @@ void transfer_message_base2(LinphoneCoreManager* marie, LinphoneCoreManager* pau compare_files(send_filepath, receive_filepath); } } - - if (!download_from_history) { - linphone_chat_message_unref(recv_msg); - } } BC_ASSERT_EQUAL(pauline->stat.number_of_LinphoneMessageInProgress,2, int, "%d"); //sent twice because of file transfer BC_ASSERT_EQUAL(pauline->stat.number_of_LinphoneMessageDelivered,1, int, "%d"); @@ -1371,7 +1369,7 @@ void lime_transfer_message_base(bool_t encrypt_file,bool_t download_file_from_st BC_ASSERT_TRUE(wait_for_until(pauline->lc,marie->lc,&marie->stat.number_of_LinphoneMessageReceivedWithFile,1, 60000)); if (marie->stat.last_received_chat_message ) { LinphoneChatMessage *recv_msg; - const LinphoneContent* content; + LinphoneContent* content; if (download_file_from_stored_msg) { LinphoneChatRoom *marie_room = linphone_core_get_chat_room(marie->lc, pauline->identity); msg_list = linphone_chat_room_get_history(marie_room,1); @@ -1391,6 +1389,7 @@ void lime_transfer_message_base(bool_t encrypt_file,bool_t download_file_from_st BC_ASSERT_PTR_NOT_NULL(linphone_content_get_key(content)); else BC_ASSERT_PTR_NULL(linphone_content_get_key(content)); + linphone_content_unref(content); if (use_file_body_handler_in_download) { linphone_chat_message_set_file_transfer_filepath(recv_msg, receive_filepath);