From 2dd2ee6e7911083c094e89b34250edf4d1b16713 Mon Sep 17 00:00:00 2001 From: Simon Morlat Date: Mon, 31 Jul 2017 16:08:56 +0200 Subject: [PATCH] fix memory leaks --- coreapi/chat.c | 8 ++++---- coreapi/chat_file_transfer.c | 2 +- coreapi/friendlist.c | 8 ++++---- coreapi/lime.c | 21 ++++++++++++++------- coreapi/linphonecore.c | 9 +++++---- coreapi/presence.c | 28 ++++++++++++++-------------- coreapi/private.h | 4 ++-- coreapi/xml.c | 6 +++--- coreapi/xmlrpc.c | 2 +- tester/message_tester.c | 12 +++++++++--- 10 files changed, 57 insertions(+), 43 deletions(-) diff --git a/coreapi/chat.c b/coreapi/chat.c index 458b8d16f..b7ecd439b 100644 --- a/coreapi/chat.c +++ b/coreapi/chat.c @@ -784,8 +784,8 @@ static const char *iscomposing_prefix = "/xsi:isComposing"; static void process_im_is_composing_notification(LinphoneChatRoom *cr, xmlparsing_context_t *xml_ctx) { char xpath_str[MAX_XPATH_LENGTH]; xmlXPathObjectPtr iscomposing_object; - const char *state_str = NULL; - const char *refresh_str = NULL; + char *state_str = NULL; + char *refresh_str = NULL; int refresh_duration = lp_config_get_int(cr->lc->config, "sip", "composing_remote_refresh_timeout", COMPOSING_DEFAULT_REMOTE_REFRESH_TIMEOUT); int i; @@ -860,8 +860,8 @@ static void process_imdn(LinphoneChatRoom *cr, xmlparsing_context_t *xml_ctx) { xmlXPathObjectPtr imdn_object; xmlXPathObjectPtr delivery_status_object; xmlXPathObjectPtr display_status_object; - const char *message_id_str = NULL; - const char *datetime_str = NULL; + char *message_id_str = NULL; + char *datetime_str = NULL; LinphoneCore *lc = linphone_chat_room_get_core(cr); LinphoneImNotifPolicy *policy = linphone_core_get_im_notif_policy(lc); diff --git a/coreapi/chat_file_transfer.c b/coreapi/chat_file_transfer.c index c1cfa822e..3d5e69378 100644 --- a/coreapi/chat_file_transfer.c +++ b/coreapi/chat_file_transfer.c @@ -335,7 +335,7 @@ static void linphone_chat_message_process_response_from_post_file(void *data, } else { /* no encryption key, transfer in plain, just copy the msg sent by server */ msg->message = ms_strdup(body); } - msg->content_type = ms_strdup("application/vnd.gsma.rcs-ft-http+xml"); + linphone_chat_message_set_content_type(msg, "application/vnd.gsma.rcs-ft-http+xml"); linphone_chat_message_ref(msg); linphone_chat_message_set_state(msg, LinphoneChatMessageStateFileTransferDone); _release_http_request(msg); diff --git a/coreapi/friendlist.c b/coreapi/friendlist.c index 52dc2ee4d..f29b626ff 100644 --- a/coreapi/friendlist.c +++ b/coreapi/friendlist.c @@ -205,9 +205,9 @@ static void linphone_friend_list_parse_multipart_related_body(LinphoneFriendList LinphoneFriend *lf; LinphoneContent *presence_part; xmlXPathObjectPtr resource_object; - const char *version_str = NULL; - const char *full_state_str = NULL; - const char *uri = NULL; + char *version_str = NULL; + char *full_state_str = NULL; + char *uri = NULL; bool_t full_state = FALSE; int version; int i; @@ -249,7 +249,7 @@ static void linphone_friend_list_parse_multipart_related_body(LinphoneFriendList resource_object = linphone_get_xml_xpath_object_for_node_list(xml_ctx, "/rlmi:list/rlmi:resource/rlmi:instance[@state=\"active\"]/.."); if ((resource_object != NULL) && (resource_object->nodesetval != NULL)) { for (i = 1; i <= resource_object->nodesetval->nodeNr; i++) { - const char *cid = NULL; + char *cid = NULL; linphone_xml_xpath_context_set_node(xml_ctx, xmlXPathNodeSetItem(resource_object->nodesetval, i-1)); cid = linphone_get_xml_text_content(xml_ctx, "./rlmi:instance/@cid"); if (cid != NULL) { diff --git a/coreapi/lime.c b/coreapi/lime.c index 08c976402..76c44556e 100644 --- a/coreapi/lime.c +++ b/coreapi/lime.c @@ -579,7 +579,7 @@ int lime_decryptMultipartMessage(void *cachedb, uint8_t *message, const char *se uint8_t selfZid[12]; /* same data but in byte buffer */ char xpath_str[MAX_XPATH_LENGTH]; limeKey_t associatedKey; - const char *peerZidHex = NULL; + char *peerZidHex = NULL; const char *sessionIndexHex = NULL; xmlparsing_context_t *xml_ctx; xmlXPathObjectPtr msg_object; @@ -635,12 +635,14 @@ int lime_decryptMultipartMessage(void *cachedb, uint8_t *message, const char *se msg_object = linphone_get_xml_xpath_object_for_node_list(xml_ctx, "/doc/msg"); if ((msg_object != NULL) && (msg_object->nodesetval != NULL)) { for (i = 1; i <= msg_object->nodesetval->nodeNr; i++) { - const char *currentZidHex; - const char *encryptedMessageb64; - const char *encryptedContentTypeb64; + char *currentZidHex; + + char *encryptedMessageb64; + char *encryptedContentTypeb64; snprintf(xpath_str, sizeof(xpath_str), "/doc/msg[%i]/pzid", i); currentZidHex = linphone_get_xml_text_content(xml_ctx, xpath_str); if ((currentZidHex != NULL) && (strcmp(currentZidHex, (char *)selfZidHex) == 0)) { + linphone_free_xml_text_content(currentZidHex); /* We found the msg node we are looking for */ snprintf(xpath_str, sizeof(xpath_str), "/doc/msg[%i]/index", i); sessionIndexHex = linphone_get_xml_text_content(xml_ctx, xpath_str); @@ -665,9 +667,9 @@ int lime_decryptMultipartMessage(void *cachedb, uint8_t *message, const char *se } break; } - if (currentZidHex != NULL) linphone_free_xml_text_content(currentZidHex); } } + if (msg_object != NULL) xmlXPathFreeObject(msg_object); } /* do we have retrieved correctly all the needed data */ @@ -766,6 +768,7 @@ bool_t linphone_chat_room_lime_available(LinphoneChatRoom *cr) { return the list of possible uris and store the selected one in the chatroom ? */ res = (lime_getCachedSndKeysByURI(zrtp_cache_db, &associatedKeys) == 0); lime_freeKeys(&associatedKeys); + ms_free(peer); return res; } } @@ -795,7 +798,10 @@ int lime_im_encryption_engine_process_incoming_message_cb(LinphoneImEncryptionEn } peerUri = linphone_address_as_string_uri_only(msg->from); selfUri = linphone_address_as_string_uri_only(msg->to); - retval = lime_decryptMultipartMessage(zrtp_cache_db, (uint8_t *)msg->message, selfUri, peerUri, &decrypted_body, &decrypted_content_type, bctbx_time_string_to_sec(lp_config_get_string(lc->config, "sip", "lime_key_validity", "0"))); + retval = lime_decryptMultipartMessage(zrtp_cache_db, (uint8_t *)msg->message, selfUri, peerUri, &decrypted_body, &decrypted_content_type, + bctbx_time_string_to_sec(lp_config_get_string(lc->config, "sip", "lime_key_validity", "0"))); + ms_free(peerUri); + ms_free(selfUri); if (retval != 0) { ms_warning("Unable to decrypt message, reason : %s", lime_error_code_to_string(retval)); if (decrypted_body) ms_free(decrypted_body); @@ -809,6 +815,7 @@ int lime_im_encryption_engine_process_incoming_message_cb(LinphoneImEncryptionEn msg->message = (char *)decrypted_body; if (decrypted_content_type != NULL) { linphone_chat_message_set_content_type(msg, decrypted_content_type); + ms_free(decrypted_content_type); } else { if (strcmp("application/cipher.vnd.gsma.rcs-ft-http+xml", msg->content_type) == 0) { linphone_chat_message_set_content_type(msg, "application/vnd.gsma.rcs-ft-http+xml"); @@ -863,7 +870,7 @@ int lime_im_encryption_engine_process_outgoing_message_cb(LinphoneImEncryptionEn ms_free(msg->message); } msg->message = (char *)crypted_body; - msg->content_type = ms_strdup(new_content_type); + linphone_chat_message_set_content_type(msg, new_content_type); } ms_free(peerUri); ms_free(selfUri); diff --git a/coreapi/linphonecore.c b/coreapi/linphonecore.c index 327ce21b2..98b6f7e98 100644 --- a/coreapi/linphonecore.c +++ b/coreapi/linphonecore.c @@ -6614,8 +6614,7 @@ void linphone_core_zrtp_cache_db_init(LinphoneCore *lc, const char *fileName) { int ret; const char *errmsg; const char *backupExtension = "_backup"; - char *backupName = reinterpret_cast(malloc(snprintf(NULL, 0, "%s%s", fileName, backupExtension) + 1)); - sprintf(backupName, "%s%s", fileName, backupExtension); + char *backupName = bctbx_strdup_printf("%s%s", fileName, backupExtension); sqlite3 *db; linphone_core_zrtp_cache_close(lc); @@ -6628,7 +6627,7 @@ void linphone_core_zrtp_cache_db_init(LinphoneCore *lc, const char *fileName) { unlink(backupName); rename(fileName, backupName); lc->zrtp_cache_db=NULL; - return; + goto end; } ret = ms_zrtp_initCache((void *)db); /* this may perform an update, check return value */ @@ -6643,11 +6642,13 @@ void linphone_core_zrtp_cache_db_init(LinphoneCore *lc, const char *fileName) { unlink(backupName); rename(fileName, backupName); lc->zrtp_cache_db = NULL; - return; + goto end; } /* everything ok, set the db pointer into core */ lc->zrtp_cache_db = db; +end: + if (backupName) bctbx_free(backupName); #endif /* SQLITE_STORAGE_ENABLED */ } diff --git a/coreapi/presence.c b/coreapi/presence.c index 45e285b59..a49550991 100644 --- a/coreapi/presence.c +++ b/coreapi/presence.c @@ -1201,8 +1201,8 @@ static int process_pidf_xml_presence_service_notes(xmlparsing_context_t *xml_ctx char xpath_str[MAX_XPATH_LENGTH]; xmlXPathObjectPtr note_object; LinphonePresenceNote *note; - const char *note_str; - const char *lang; + char *note_str; + char *lang; int i; snprintf(xpath_str, sizeof(xpath_str), "%s[%i]/pidf:note", service_prefix, service_idx); @@ -1231,10 +1231,10 @@ static int process_pidf_xml_presence_services(xmlparsing_context_t *xml_ctx, Lin xmlXPathObjectPtr service_object; xmlXPathObjectPtr pidfonline_object; LinphonePresenceService *service; - const char *basic_status_str; - const char *service_id_str; - const char *timestamp_str; - const char *contact_str; + char *basic_status_str; + char *service_id_str; + char *timestamp_str; + char *contact_str; LinphonePresenceBasicStatus basic_status; int i; @@ -1306,7 +1306,7 @@ static int process_pidf_xml_presence_person_activities(xmlparsing_context_t *xml xmlXPathObjectPtr activities_object; xmlNodePtr activity_node; LinphonePresenceActivity *activity; - const char *description; + char *description; int i, j; int err = 0; @@ -1321,7 +1321,7 @@ static int process_pidf_xml_presence_person_activities(xmlparsing_context_t *xml activity_node = activities_object->nodesetval->nodeTab[j]; if ((activity_node->name != NULL) && (is_valid_activity_name((const char *)activity_node->name) == TRUE)) { LinphonePresenceActivityType acttype; - description = (const char *)xmlNodeGetContent(activity_node); + description = (char *)xmlNodeGetContent(activity_node); if ((description != NULL) && (description[0] == '\0')) { linphone_free_xml_text_content(description); description = NULL; @@ -1348,8 +1348,8 @@ static int process_pidf_xml_presence_person_notes(xmlparsing_context_t *xml_ctx, char xpath_str[MAX_XPATH_LENGTH]; xmlXPathObjectPtr note_object; LinphonePresenceNote *note; - const char *note_str; - const char *lang; + char *note_str; + char *lang; int i; snprintf(xpath_str, sizeof(xpath_str), "%s[%i]/rpid:activities/rpid:note", person_prefix, person_idx); @@ -1395,8 +1395,8 @@ static int process_pidf_xml_presence_persons(xmlparsing_context_t *xml_ctx, Linp char xpath_str[MAX_XPATH_LENGTH]; xmlXPathObjectPtr person_object; LinphonePresencePerson *person; - const char *person_id_str; - const char *person_timestamp_str; + char *person_id_str; + char *person_timestamp_str; time_t timestamp; int i; int err = 0; @@ -1444,8 +1444,8 @@ static int process_pidf_xml_presence_notes(xmlparsing_context_t *xml_ctx, Linpho char xpath_str[MAX_XPATH_LENGTH]; xmlXPathObjectPtr note_object; LinphonePresenceNote *note; - const char *note_str; - const char *lang; + char *note_str; + char *lang; int i; note_object = linphone_get_xml_xpath_object_for_node_list(xml_ctx, "/pidf:presence/pidf:note"); diff --git a/coreapi/private.h b/coreapi/private.h index 73897d995..b95916c60 100644 --- a/coreapi/private.h +++ b/coreapi/private.h @@ -1700,8 +1700,8 @@ void linphone_xmlparsing_genericxml_error(void *ctx, const char *fmt, ...); int linphone_create_xml_xpath_context(xmlparsing_context_t *xml_ctx); void linphone_xml_xpath_context_set_node(xmlparsing_context_t *xml_ctx, xmlNodePtr node); char * linphone_get_xml_text_content(xmlparsing_context_t *xml_ctx, const char *xpath_expression); -const char * linphone_get_xml_attribute_text_content(xmlparsing_context_t *xml_ctx, const char *xpath_expression, const char *attribute_name); -void linphone_free_xml_text_content(const char *text); +char * linphone_get_xml_attribute_text_content(xmlparsing_context_t *xml_ctx, const char *xpath_expression, const char *attribute_name); +void linphone_free_xml_text_content(char *text); xmlXPathObjectPtr linphone_get_xml_xpath_object_for_node_list(xmlparsing_context_t *xml_ctx, const char *xpath_expression); void linphone_xml_xpath_context_init_carddav_ns(xmlparsing_context_t *xml_ctx); diff --git a/coreapi/xml.c b/coreapi/xml.c index 4e2e154a4..4443b8610 100644 --- a/coreapi/xml.c +++ b/coreapi/xml.c @@ -94,7 +94,7 @@ char * linphone_get_xml_text_content(xmlparsing_context_t *xml_ctx, const char * return (char *)text; } -const char * linphone_get_xml_attribute_text_content(xmlparsing_context_t *xml_ctx, const char *xpath_expression, const char *attribute_name) { +char * linphone_get_xml_attribute_text_content(xmlparsing_context_t *xml_ctx, const char *xpath_expression, const char *attribute_name) { xmlXPathObjectPtr xpath_obj; xmlChar *text = NULL; @@ -118,10 +118,10 @@ const char * linphone_get_xml_attribute_text_content(xmlparsing_context_t *xml_c xmlXPathFreeObject(xpath_obj); } - return (const char *)text; + return (char*)text; } -void linphone_free_xml_text_content(const char *text) { +void linphone_free_xml_text_content(char *text) { xmlFree((xmlChar *)text); } diff --git a/coreapi/xmlrpc.c b/coreapi/xmlrpc.c index 682bb8f24..221876f15 100644 --- a/coreapi/xmlrpc.c +++ b/coreapi/xmlrpc.c @@ -196,7 +196,7 @@ static void parse_valid_xml_rpc_response(LinphoneXmlRpcRequest *request, const c request->status = LinphoneXmlRpcStatusFailed; xml_ctx->doc = xmlReadDoc((const unsigned char*)response_body, 0, NULL, 0); if (xml_ctx->doc != NULL) { - const char *response_str = NULL; + char *response_str = NULL; if (linphone_create_xml_xpath_context(xml_ctx) < 0) goto end; switch (request->response.type) { case LinphoneXmlRpcArgInt: diff --git a/tester/message_tester.c b/tester/message_tester.c index 972dfd58e..d3293b1c7 100644 --- a/tester/message_tester.c +++ b/tester/message_tester.c @@ -877,6 +877,7 @@ static int enable_lime_for_message_test(LinphoneCoreManager *marie, LinphoneCore int ret = 0; char* paulineUri = NULL; char* marieUri = NULL; + char *tmp; if (!linphone_core_lime_available(marie->lc) || !linphone_core_lime_available(pauline->lc)) { ms_warning("Lime not available, skiping"); @@ -891,8 +892,12 @@ static int enable_lime_for_message_test(LinphoneCoreManager *marie, LinphoneCore lp_config_set_int(pauline->lc->config, "sip", "zrtp_cache_migration_done", TRUE); /* create temporary cache files: setting the database_path will create and initialise the files */ - remove(bc_tester_file("tmpZIDCacheMarie.sqlite")); - remove(bc_tester_file("tmpZIDCachePauline.sqlite")); + tmp = bc_tester_file("tmpZIDCacheMarie.sqlite"); + remove(tmp); + bc_free(tmp); + tmp = bc_tester_file("tmpZIDCachePauline.sqlite"); + remove(tmp); + bc_free(tmp); filepath = bc_tester_file("tmpZIDCacheMarie.sqlite"); linphone_core_set_zrtp_secrets_file(marie->lc, filepath); bc_free(filepath); @@ -920,7 +925,8 @@ static int enable_lime_for_message_test(LinphoneCoreManager *marie, LinphoneCore sqlite3_free(errmsg); return -1; } - + ms_free(paulineUri); + ms_free(marieUri); return 0; #else /* SQLITE_STORAGE_ENABLED */