From ae313e7020eb178344fcc0663888a5fe92f2af11 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Wed, 29 Jun 2016 14:41:25 +0200 Subject: [PATCH] Fix some memory leaks --- coreapi/carddav.c | 12 +++++++++--- coreapi/friend.c | 38 ++++++++++++++++++++++---------------- coreapi/vcard.cc | 2 ++ 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/coreapi/carddav.c b/coreapi/carddav.c index cac882fc5..c61ef8905 100644 --- a/coreapi/carddav.c +++ b/coreapi/carddav.c @@ -106,6 +106,7 @@ static void linphone_carddav_response_free(LinphoneCardDavResponse *response) { } static void linphone_carddav_vcards_pulled(LinphoneCardDavContext *cdc, bctbx_list_t *vCards) { + bctbx_list_t *vCards_remember = vCards; if (vCards != NULL && bctbx_list_size(vCards) > 0) { bctbx_list_t *friends = cdc->friend_list->friends; while (vCards) { @@ -158,7 +159,7 @@ static void linphone_carddav_vcards_pulled(LinphoneCardDavContext *cdc, bctbx_li } vCards = bctbx_list_next(vCards); } - bctbx_list_free_with_data(vCards, (void (*)(void *))linphone_carddav_response_free); + bctbx_list_free_with_data(vCards_remember, (void (*)(void *))linphone_carddav_response_free); } linphone_carddav_server_to_client_sync_done(cdc, TRUE, NULL); } @@ -190,6 +191,9 @@ static bctbx_list_t* parse_vcards_from_xml_response(const char *body) { response->vcard = ms_strdup(vcard); result = bctbx_list_append(result, response); ms_debug("Added vCard object with eTag %s, URL %s and vCard %s", etag, url, vcard); + linphone_free_xml_text_content(etag); + linphone_free_xml_text_content(url); + linphone_free_xml_text_content(vcard); } } } @@ -281,6 +285,8 @@ static bctbx_list_t* parse_vcards_etags_from_xml_response(const char *body) { response->url = ms_strdup(url); result = bctbx_list_append(result, response); ms_debug("Added vCard object with eTag %s and URL %s", etag, url); + linphone_free_xml_text_content(etag); + linphone_free_xml_text_content(url); } } } @@ -734,11 +740,11 @@ static LinphoneCardDavQuery* linphone_carddav_create_addressbook_multiget_query( if (response) { char temp_body[300]; snprintf(temp_body, sizeof(temp_body), "%s", response->url); - sprintf(body, "%s%s", body, temp_body); + strcat(body, temp_body); iterator = bctbx_list_next(iterator); } } - sprintf(body, "%s%s", body, ""); + strcat(body, ""); query->body = ms_strdup(body); ms_free(body); diff --git a/coreapi/friend.c b/coreapi/friend.c index f08ef3b6f..1a105dd54 100644 --- a/coreapi/friend.c +++ b/coreapi/friend.c @@ -218,7 +218,7 @@ int linphone_friend_set_address(LinphoneFriend *lf, const LinphoneAddress *addr) if (lf->uri != NULL) linphone_address_unref(lf->uri); lf->uri = fr; - vcard = linphone_friend_get_vcard(lf); + vcard = lf->vcard; if (vcard) { linphone_vcard_edit_main_sip_address(vcard, linphone_address_as_string_uri_only(fr)); } @@ -237,7 +237,7 @@ void linphone_friend_add_address(LinphoneFriend *lf, const LinphoneAddress *addr lf->uri = fr; } - vcard = linphone_friend_get_vcard(lf); + vcard = lf->vcard; if (!vcard) { return; } @@ -255,7 +255,7 @@ bctbx_list_t* linphone_friend_get_addresses(LinphoneFriend *lf) { return NULL; } - vcard = linphone_friend_get_vcard(lf); + vcard = lf->vcard; if (!vcard) { return lf->uri ? bctbx_list_append(addresses, lf->uri) : NULL; } @@ -280,7 +280,7 @@ void linphone_friend_remove_address(LinphoneFriend *lf, const LinphoneAddress *a return; } - vcard = linphone_friend_get_vcard(lf); + vcard = lf->vcard; if (!vcard) { return; } @@ -294,7 +294,7 @@ void linphone_friend_add_phone_number(LinphoneFriend *lf, const char *phone) { return; } - vcard = linphone_friend_get_vcard(lf); + vcard = lf->vcard; if (!vcard) { return; } @@ -309,7 +309,7 @@ bctbx_list_t* linphone_friend_get_phone_numbers(LinphoneFriend *lf) { return NULL; } - vcard = linphone_friend_get_vcard(lf); + vcard = lf->vcard; if (!vcard) { return NULL; } @@ -323,7 +323,7 @@ void linphone_friend_remove_phone_number(LinphoneFriend *lf, const char *phone) return; } - vcard = linphone_friend_get_vcard(lf); + vcard = lf->vcard; if (!vcard) { return; } @@ -336,16 +336,18 @@ int linphone_friend_set_name(LinphoneFriend *lf, const char *name){ LinphoneVcard *vcard = NULL; bool_t vcard_created = FALSE; - vcard = linphone_friend_get_vcard(lf); + vcard = lf->vcard; if (!vcard) { linphone_friend_create_vcard(lf, name); - vcard = linphone_friend_get_vcard(lf); + vcard = lf->vcard; vcard_created = TRUE; } if (vcard) { linphone_vcard_set_full_name(vcard, name); if (fr && vcard_created) { // SIP address wasn't set yet, let's do it - linphone_vcard_edit_main_sip_address(vcard, linphone_address_as_string_uri_only(fr)); + char *address = linphone_address_as_string_uri_only(fr); + linphone_vcard_edit_main_sip_address(vcard, address); + ms_free(address); } } @@ -1056,14 +1058,14 @@ bool_t linphone_friend_create_vcard(LinphoneFriend *fr, const char *name) { LinphoneFriend *linphone_friend_new_from_vcard(LinphoneVcard *vcard) { LinphoneAddress* linphone_address = NULL; LinphoneFriend *fr; - const char *name = NULL; + char *name = NULL; bctbx_list_t *sipAddresses = NULL; if (vcard == NULL) { ms_error("Cannot create friend from null vcard"); return NULL; } - name = linphone_vcard_get_full_name(vcard); + name = ms_strdup(linphone_vcard_get_full_name(vcard)); sipAddresses = linphone_vcard_get_sip_addresses(vcard); fr = linphone_friend_new(); @@ -1078,12 +1080,13 @@ LinphoneFriend *linphone_friend_new_from_vcard(LinphoneVcard *vcard) { linphone_friend_set_address(fr, linphone_address); linphone_address_unref(linphone_address); } - ms_free(sipAddresses); + bctbx_list_free(sipAddresses); } + fr->vcard = vcard; if (name) { linphone_friend_set_name(fr, name); } - fr->vcard = vcard; + ms_free(name); return fr; } @@ -1306,6 +1309,7 @@ void linphone_core_store_friend_in_db(LinphoneCore *lc, LinphoneFriend *lf) { char *buf; int store_friends = lp_config_get_int(lc->config, "misc", "store_friends", 1); LinphoneVcard *vcard = linphone_friend_get_vcard(lf); + char *address = NULL; if (!store_friends) { return; @@ -1321,10 +1325,11 @@ void linphone_core_store_friend_in_db(LinphoneCore *lc, LinphoneFriend *lf) { linphone_core_store_friends_list_in_db(lc, lf->friend_list); } + address = linphone_address_as_string(linphone_friend_get_address(lf)); if (lf->storage_id > 0) { buf = sqlite3_mprintf("UPDATE friends SET friend_list_id=%u,sip_uri=%Q,subscribe_policy=%i,send_subscribe=%i,ref_key=%Q,vCard=%Q,vCard_etag=%Q,vCard_url=%Q,presence_received=%i WHERE (id = %u);", lf->friend_list->storage_id, - linphone_address_as_string(linphone_friend_get_address(lf)), + address, lf->pol, lf->subscribe, lf->refkey, @@ -1337,7 +1342,7 @@ void linphone_core_store_friend_in_db(LinphoneCore *lc, LinphoneFriend *lf) { } else { buf = sqlite3_mprintf("INSERT INTO friends VALUES(NULL,%u,%Q,%i,%i,%Q,%Q,%Q,%Q,%i);", lf->friend_list->storage_id, - linphone_address_as_string(linphone_friend_get_address(lf)), + address, lf->pol, lf->subscribe, lf->refkey, @@ -1347,6 +1352,7 @@ void linphone_core_store_friend_in_db(LinphoneCore *lc, LinphoneFriend *lf) { lf->presence_received ); } + ms_free(address); linphone_sql_request_generic(lc->friends_db, buf); sqlite3_free(buf); diff --git a/coreapi/vcard.cc b/coreapi/vcard.cc index 1aa7dd17a..95b13dcd9 100644 --- a/coreapi/vcard.cc +++ b/coreapi/vcard.cc @@ -268,6 +268,7 @@ void linphone_vcard_set_etag(LinphoneVcard *vCard, const char * etag) { } if (vCard->etag) { ms_free(vCard->etag); + vCard->etag = NULL; } vCard->etag = ms_strdup(etag); } @@ -283,6 +284,7 @@ void linphone_vcard_set_url(LinphoneVcard *vCard, const char * url) { } if (vCard->url) { ms_free(vCard->url); + vCard->url = NULL; } vCard->url = ms_strdup(url); }