Fix memory leak 20/93220/2
authorjusung son <jusung07.son@samsung.com>
Fri, 21 Oct 2016 05:57:41 +0000 (14:57 +0900)
committerjusung son <jusung07.son@samsung.com>
Fri, 21 Oct 2016 06:09:38 +0000 (15:09 +0900)
Change-Id: I022d60b85aaca201781e58a6e0caf95a7daf079e
Signed-off-by: jusung son <jusung07.son@samsung.com>
src/data-control-bulk.c [changed mode: 0644->0755]
src/data-control-internal.c
src/data-control-map.c
src/data-control-noti.c [changed mode: 0644->0755]
src/data-control-provider.c
src/data-control-sql.c

old mode 100644 (file)
new mode 100755 (executable)
index a52e53b..587b264
@@ -125,6 +125,7 @@ int datacontrol_bulk_result_data_add(data_control_bulk_result_data_h result_data
        if (result_data != NULL) {
                result_data_item->result_data = bundle_dup(result_data);
                if (result_data_item->result_data == NULL) {
+                       free(result_data_item);
                        LOGE("fail to alloc result_data");
                        return DATACONTROL_ERROR_OUT_OF_MEMORY;
                }
index 50b5100d394a2b0ae0da1d36143747ddcb35795c..440c3f2ad600aa6d06ba1b72a890dcbb12df3947 100755 (executable)
@@ -84,7 +84,7 @@ int _datacontrol_send_async(int sockfd, bundle *kb, void *extra_data, datacontro
        bundle *bulk_data;
        data_control_bulk_data_h bulk_data_h = NULL;
        bundle_raw *encode_data = NULL;
-       int encode_datalen;
+       int encode_datalen = 0;
 
        LOGD("send async ~~~");
 
@@ -155,6 +155,8 @@ int _datacontrol_send_async(int sockfd, bundle *kb, void *extra_data, datacontro
                                ret = DATACONTROL_ERROR_IO_ERROR;
                                goto out;
                        }
+                       encode_datalen = 0;
+                       bundle_free_encoded_rawdata(&encode_data);
                }
        }
 
@@ -569,6 +571,7 @@ int _request_appsvc_run(const char *caller_id, const char *callee_id)
        } while (count < TRY_COUNT);
 
        if (count >= TRY_COUNT) {
+               bundle_free(arg_list);
                LOGE("unable to launch service: %d", pid);
                return DATACONTROL_ERROR_IO_ERROR;
        }
index 1552bd248f60d84f4ed2eca1bfa5ee168d62a49d..7431686199df325a0ae51d65a56a9ef258b5162f 100755 (executable)
@@ -206,11 +206,16 @@ static void __remove_map_request_info(int request_id, map_response_cb_s *map_dc)
 {
 
        datacontrol_consumer_request_info temp_request_info;
+       datacontrol_consumer_request_info *data;
+
        temp_request_info.request_id = request_id;
        GList *list = g_list_find_custom(map_dc->request_info_list, &temp_request_info,
                        (GCompareFunc)_consumer_request_compare_cb);
-       if (list != NULL)
+       if (list != NULL) {
+               data = (datacontrol_consumer_request_info *)list->data;
                map_dc->request_info_list = g_list_remove(map_dc->request_info_list, list->data);
+               free(data);
+       }
 }
 
 static int __map_handle_cb(int fd, bundle *b, int request_type, int request_id, appsvc_result_val res, void *data)
@@ -223,6 +228,7 @@ static int __map_handle_cb(int fd, bundle *b, int request_type, int request_id,
        int result_list_len = 0;
        int provider_result = 0;
        int value_count = 0;
+       int i;
        char **value_list = NULL;
        const char *p = NULL;
        data_control_bulk_result_data_h bulk_results = NULL;
@@ -310,6 +316,17 @@ static int __map_handle_cb(int fd, bundle *b, int request_type, int request_id,
                ret = DATACONTROL_ERROR_INVALID_PARAMETER;
        }
 
+       if (bulk_results)
+               datacontrol_bulk_result_data_destroy(bulk_results);
+
+       if (value_list) {
+               for (i = 0; i < value_count; i++) {
+                       if (value_list[i] != NULL)
+                               free(value_list[i]);
+               }
+               free(value_list);
+       }
+
        return ret;
 }
 
@@ -449,6 +466,7 @@ static int __map_request_provider(datacontrol_h provider, datacontrol_request_ty
        int count = 0;
        const int TRY_COUNT = 2;
        const struct timespec TRY_SLEEP_TIME = { 0, 1000 * 1000 * 1000 };
+       bundle *send_data;
 
        LOGI("Map Data control request, type: %d, request id: %d", type, request_id);
 
@@ -516,14 +534,20 @@ static int __map_request_provider(datacontrol_h provider, datacontrol_request_ty
                return DATACONTROL_ERROR_INVALID_PARAMETER;
        }
 
-       bundle_add_str(request_data, OSP_K_DATACONTROL_PROTOCOL_VERSION, OSP_V_VERSION_2_1_0_3);
-       bundle_add_str(request_data, AUL_K_CALLER_APPID, caller_app_id);
+       send_data = bundle_dup(request_data);
+       if (send_data == NULL) {
+               LOGE("fail to alloc result_data");
+               return DATACONTROL_ERROR_OUT_OF_MEMORY;
+       }
+
+       bundle_add_str(send_data, OSP_K_DATACONTROL_PROTOCOL_VERSION, OSP_V_VERSION_2_1_0_3);
+       bundle_add_str(send_data, AUL_K_CALLER_APPID, caller_app_id);
 
        snprintf(datacontrol_request_operation, MAX_LEN_DATACONTROL_REQ_TYPE, "%d", (int)(type));
-       bundle_add_str(request_data, OSP_K_DATACONTROL_REQUEST_TYPE, datacontrol_request_operation);
+       bundle_add_str(send_data, OSP_K_DATACONTROL_REQUEST_TYPE, datacontrol_request_operation);
 
        snprintf(req_id, 32, "%d", request_id);
-       bundle_add_str(request_data, OSP_K_REQUEST_ID, req_id);
+       bundle_add_str(send_data, OSP_K_REQUEST_ID, req_id);
 
        LOGI("caller_id %s, app_id %s", caller_app_id, app_id);
 
@@ -531,18 +555,22 @@ static int __map_request_provider(datacontrol_h provider, datacontrol_request_ty
                datacontrol_socket_info *socket_info = g_hash_table_lookup(__socket_pair_hash, provider->provider_id);
                if (socket_info == NULL) {
                        ret = _request_appsvc_run(caller_app_id, app_id);
-                       if (ret != DATACONTROL_ERROR_NONE)
+                       if (ret != DATACONTROL_ERROR_NONE) {
+                               bundle_free(send_data);
                                return ret;
+                       }
 
                        socket_info = _add_watch_on_socket_info(caller_app_id, app_id, "consumer", __recv_map_message, data);
-                       if (socket_info == NULL)
+                       if (socket_info == NULL) {
+                               bundle_free(send_data);
                                return DATACONTROL_ERROR_IO_ERROR;
+                       }
 
                        g_hash_table_insert(__socket_pair_hash, strdup(provider->provider_id), socket_info);
                }
 
                LOGI("send data from consumer !!!");
-               ret = _datacontrol_send_async(socket_info->socket_fd, request_data, extra_data, type, NULL);
+               ret = _datacontrol_send_async(socket_info->socket_fd, send_data, extra_data, type, NULL);
                if (ret != DATACONTROL_ERROR_NONE)
                        g_hash_table_remove(__socket_pair_hash, provider->provider_id);
                else
@@ -551,6 +579,7 @@ static int __map_request_provider(datacontrol_h provider, datacontrol_request_ty
                nanosleep(&TRY_SLEEP_TIME, 0);
        } while (ret != DATACONTROL_ERROR_NONE && count < TRY_COUNT);
 
+       bundle_free(send_data);
        return ret;
 }
 
old mode 100644 (file)
new mode 100755 (executable)
index 81214f4..424e2c1
@@ -116,7 +116,7 @@ static void __free_provider_info(
                        free(provider_info->data_id);
                if (provider_info->monitor_id > 0)
                        g_dbus_connection_signal_unsubscribe(_get_dbus_connection(), provider_info->monitor_id);
-               g_list_free(provider_info->cb_list);
+               g_list_free_full(provider_info->cb_list, free);
                free(provider_info);
        }
 }
@@ -174,6 +174,7 @@ static void __call_result_callback(int callback_id, int callback_result)
        add_callback_result_cb_info_s find_info;
        datacontrol_h provider;
        GList *find_list;
+
        find_info.callback_id = callback_id;
        find_list = g_list_find_custom(__add_callback_result_cb_list, &find_info,
                        (GCompareFunc)__callback_result_info_compare_cb);
@@ -188,8 +189,9 @@ static void __call_result_callback(int callback_id, int callback_result)
                                callback_result,
                                callback_id,
                                result_cb_info->user_data);
-               __add_callback_result_cb_list = g_list_remove_link(__add_callback_result_cb_list, find_list);
-               __free_result_cb_info((add_callback_result_cb_info_s *)find_list->data);
+
+               __add_callback_result_cb_list = g_list_remove(__add_callback_result_cb_list, find_list->data);
+               __free_result_cb_info(result_cb_info);
                _destroy_datacontrol_h(provider);
        } else {
                LOGE("add_callback_result_cb_info_s is null");
@@ -429,6 +431,7 @@ int datacontrol_remove_data_change_cb(datacontrol_h provider, int callback_id)
        provider_info_s info;
        provider_info_s *target_provider_info;
        add_callback_result_cb_info_s result_cb_info;
+       add_callback_result_cb_info_s *tmp;
        GList *provider_list = NULL;
        GList *callback_list = NULL;
        GList *result_cb_list = NULL;
@@ -459,6 +462,7 @@ int datacontrol_remove_data_change_cb(datacontrol_h provider, int callback_id)
        removed_cb_info = (changed_cb_info_s *)callback_list->data;
        target_provider_info->cb_list
                = g_list_remove(target_provider_info->cb_list, removed_cb_info);
+       free(removed_cb_info);
 
        if (g_list_length(target_provider_info->cb_list) == 0) {
                unique_id = g_dbus_connection_get_unique_name(_get_dbus_connection());
@@ -475,16 +479,17 @@ int datacontrol_remove_data_change_cb(datacontrol_h provider, int callback_id)
                        return ret;
                }
 
-               __changed_provider_list = g_list_remove_link(__changed_provider_list, provider_list);
-               __free_provider_info((provider_info_s *)provider_list->data);
+               __changed_provider_list = g_list_remove(__changed_provider_list, target_provider_info);
+               __free_provider_info(target_provider_info);
        }
 
        result_cb_info.callback_id = callback_id;
        result_cb_list = g_list_find_custom(__add_callback_result_cb_list, &result_cb_info,
                        (GCompareFunc)__callback_result_info_compare_cb);
        if (result_cb_list) {
-               __add_callback_result_cb_list = g_list_remove_link(__add_callback_result_cb_list, result_cb_list);
-               __free_result_cb_info((add_callback_result_cb_info_s *)result_cb_list->data);
+               tmp = (add_callback_result_cb_info_s *)result_cb_list->data;
+               __add_callback_result_cb_list = g_list_remove(__add_callback_result_cb_list, result_cb_list->data);
+               __free_result_cb_info(tmp);
        }
        return DATACONTROL_ERROR_NONE;
 }
index 925a3673ac964f7bb4d53bc49f740dcf05c0bb0a..e340e38de9c24331ffdbc1f27cfa8b28007836f0 100755 (executable)
@@ -142,9 +142,19 @@ static void __free_consumer_info(const gchar *name)
        g_bus_unwatch_name(info->monitor_id);
 
        __noti_consumer_app_list = g_list_remove(__noti_consumer_app_list, find_list->data);
+
+       free(info);
        LOGI("__free_consumer_info done");
 }
 
+static void __free_bundle_data(gpointer data)
+{
+       if (data) {
+               bundle_free(data);
+               data = NULL;
+       }
+}
+
 static void __free_data(gpointer data)
 {
        if (data) {
@@ -156,7 +166,7 @@ static void __free_data(gpointer data)
 static void __initialize_provider(void)
 {
        int result;
-       __request_table = g_hash_table_new_full(g_int_hash, g_int_equal, __free_data, __free_data);
+       __request_table = g_hash_table_new_full(g_int_hash, g_int_equal, __free_data, __free_bundle_data);
        __socket_pair_hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, _socket_info_free);
        result = __create_consumer_list_db();
        if (result != DATACONTROL_ERROR_NONE)
@@ -1014,6 +1024,16 @@ static int __init_changed_noti_consumer_list()
                                consumer_info,
                                NULL);
 
+               if (consumer_info->monitor_id == 0) {
+                       LOGE("g_bus_watch_name_on_connection fail");
+
+                       free(consumer_info->appid);
+                       free(consumer_info->object_path);
+                       free(consumer_info->unique_id);
+                       free(consumer_info);
+                       continue;
+               }
+
                LOGI("noti consumer_app_list append %s", consumer_info->object_path);
                __noti_consumer_app_list =
                        g_list_append(__noti_consumer_app_list, consumer_info);
@@ -1324,7 +1344,7 @@ err:
        if (key)
                free(key);
        if (value)
-               free(value);
+               bundle_free(value);
 
        return DATACONTROL_ERROR_IO_ERROR;
 }
@@ -1625,6 +1645,8 @@ out:
 
        if (provider)
                free(provider);
+       if (path)
+               free(path);
 
        return result;
 }
@@ -2032,7 +2054,8 @@ static int __send_signal_to_consumer(datacontrol_h provider,
                                        err->message);
                        g_error_free(err);
                }
-               return DATACONTROL_ERROR_IO_ERROR;
+               result = DATACONTROL_ERROR_IO_ERROR;
+               goto out;
        }
 
 out:
@@ -2091,11 +2114,15 @@ int datacontrol_provider_remove_data_change_consumer_filter_cb(int callback_id)
 {
        GList *find_list;
        changed_noti_consumer_filter_info_s filter_info;
+       changed_noti_consumer_filter_info_s *temp;
+
        filter_info.callback_id = callback_id;
        find_list = g_list_find_custom(__noti_consumer_filter_info_list, &filter_info,
                        (GCompareFunc)__data_changed_filter_cb_info_compare_cb);
        if (find_list != NULL) {
+               temp = (changed_noti_consumer_filter_info_s *)find_list->data;
                __noti_consumer_filter_info_list = g_list_remove(__noti_consumer_filter_info_list, find_list->data);
+               free(temp);
        } else {
                LOGE("invalid callback_id : %d", callback_id);
                return DATACONTROL_ERROR_INVALID_PARAMETER;
@@ -2158,9 +2185,11 @@ int datacontrol_provider_foreach_data_change_consumer(
                        break;
        }
 out:
-       sqlite3_reset(stmt);
-       sqlite3_clear_bindings(stmt);
-       sqlite3_finalize(stmt);
+       if (stmt) {
+               sqlite3_reset(stmt);
+               sqlite3_clear_bindings(stmt);
+               sqlite3_finalize(stmt);
+       }
 
        return ret;
 }
index 781e5e0ef35d5609a8acaed4cddc65352d01b1fb..5e666b082d5cf95be651828e526c1c11222bb5c4 100755 (executable)
@@ -154,11 +154,16 @@ static void __remove_sql_request_info(int request_id, sql_response_cb_s *sql_dc)
 {
 
        datacontrol_consumer_request_info temp_request_info;
+       datacontrol_consumer_request_info *temp_data;
+
        temp_request_info.request_id = request_id;
        GList *list = g_list_find_custom(sql_dc->request_info_list, &temp_request_info,
                        (GCompareFunc)_consumer_request_compare_cb);
-       if (list != NULL)
+       if (list != NULL) {
+               temp_data = (datacontrol_consumer_request_info *)list->data;
                sql_dc->request_info_list = g_list_remove(sql_dc->request_info_list, list->data);
+               free(temp_data);
+       }
 
 }
 
@@ -616,7 +621,7 @@ static gboolean __consumer_recv_sql_message(GIOChannel *channel,
        guint nb;
        const char *p = NULL;
        int request_id;
-       bundle *kb;
+       bundle *kb = NULL;
        sql_response_cb_s *sql_dc;
        GList *itr;
        datacontrol_consumer_request_info *request_info;
@@ -678,12 +683,15 @@ static gboolean __consumer_recv_sql_message(GIOChannel *channel,
                                                != DATACONTROL_ERROR_NONE)
                                goto error;
                        __remove_sql_request_info(request_id, data);
+                       bundle_free(kb);
                }
        }
        return TRUE;
 error:
        if (buf)
                free(buf);
+       if (kb)
+               bundle_free(kb);
 
        if (((sql_response_cb_s *)data) != NULL) {
                LOGE("g_hash_table_remove");
@@ -723,7 +731,7 @@ int __datacontrol_send_sql_async(int sockfd, bundle *kb, void *extra_data, datac
        bundle *bulk_data;
        data_control_bulk_data_h bulk_data_h = NULL;
        bundle_raw *encode_data = NULL;
-       int encode_datalen;
+       int encode_datalen = 0;
 
        LOGD("send async ~~~");
 
@@ -793,6 +801,8 @@ int __datacontrol_send_sql_async(int sockfd, bundle *kb, void *extra_data, datac
                                ret = DATACONTROL_ERROR_IO_ERROR;
                                goto out;
                        }
+                       encode_datalen = 0;
+                       bundle_free_encoded_rawdata(&encode_data);
                }
        }
 
@@ -820,7 +830,7 @@ static int __sql_request_provider(datacontrol_h provider, datacontrol_request_ty
        const int TRY_COUNT = 2;
        const struct timespec TRY_SLEEP_TIME = { 0, 1000 * 1000 * 1000 };
        char *socket_info_key;
-
+       bundle *send_data;
 
        LOGI("SQL Data control request, type: %d, request id: %d", type, request_id);
 
@@ -892,14 +902,20 @@ static int __sql_request_provider(datacontrol_h provider, datacontrol_request_ty
                return DATACONTROL_ERROR_INVALID_PARAMETER;
        }
 
-       bundle_add_str(request_data, OSP_K_DATACONTROL_PROTOCOL_VERSION, OSP_V_VERSION_2_1_0_3);
-       bundle_add_str(request_data, AUL_K_CALLER_APPID, caller_app_id);
+       send_data = bundle_dup(request_data);
+       if (send_data == NULL) {
+               LOGE("fail to alloc result_data");
+               return DATACONTROL_ERROR_OUT_OF_MEMORY;
+       }
+
+       bundle_add_str(send_data, OSP_K_DATACONTROL_PROTOCOL_VERSION, OSP_V_VERSION_2_1_0_3);
+       bundle_add_str(send_data, AUL_K_CALLER_APPID, caller_app_id);
 
        snprintf(datacontrol_request_operation, MAX_LEN_DATACONTROL_REQ_TYPE, "%d", (int)(type));
-       bundle_add_str(request_data, OSP_K_DATACONTROL_REQUEST_TYPE, datacontrol_request_operation);
+       bundle_add_str(send_data, OSP_K_DATACONTROL_REQUEST_TYPE, datacontrol_request_operation);
 
        snprintf(req_id, 32, "%d", request_id);
-       bundle_add_str(request_data, OSP_K_REQUEST_ID, req_id);
+       bundle_add_str(send_data, OSP_K_REQUEST_ID, req_id);
 
        do {
                datacontrol_socket_info *socket_info = g_hash_table_lookup(__socket_pair_hash, provider->provider_id);
@@ -908,25 +924,28 @@ static int __sql_request_provider(datacontrol_h provider, datacontrol_request_ty
                        ret = _request_appsvc_run(caller_app_id, app_id);
                        if (ret != DATACONTROL_ERROR_NONE) {
                                LOGE("_request_appsvc_run error !!!");
+                               bundle_free(send_data);
                                return ret;
                        }
 
                        socket_info = _add_watch_on_socket_info(caller_app_id, app_id, "consumer", __consumer_recv_sql_message, response_cb_data);
                        if (socket_info == NULL) {
                                LOGE("_get_socket_info error !!!");
+                               bundle_free(send_data);
                                return DATACONTROL_ERROR_IO_ERROR;
                        }
 
                        socket_info_key = strdup(provider->provider_id);
                        if (socket_info_key == NULL) {
                                LOGE("Out of memory. can not dup select map file.");
+                               bundle_free(send_data);
                                return DATACONTROL_ERROR_IO_ERROR;
                        }
                        g_hash_table_insert(__socket_pair_hash, socket_info_key, socket_info);
                }
 
                LOGE("send data from consumer");
-               ret = __datacontrol_send_sql_async(socket_info->socket_fd, request_data, extra_data, type, NULL);
+               ret = __datacontrol_send_sql_async(socket_info->socket_fd, send_data, extra_data, type, NULL);
                if (ret != DATACONTROL_ERROR_NONE)
                        g_hash_table_remove(__socket_pair_hash, provider->provider_id);
                else
@@ -936,6 +955,7 @@ static int __sql_request_provider(datacontrol_h provider, datacontrol_request_ty
                nanosleep(&TRY_SLEEP_TIME, 0);
        } while (ret != DATACONTROL_ERROR_NONE && count < TRY_COUNT);
 
+       bundle_free(send_data);
        return ret;
 }