Fix memory leak 03/126003/1 accepted/tizen/3.0/common/20170508.152946 accepted/tizen/3.0/ivi/20170508.050222 accepted/tizen/3.0/mobile/20170508.050136 accepted/tizen/3.0/tv/20170508.050156 accepted/tizen/3.0/wearable/20170508.050208 submit/tizen_3.0-common/20170508.080135 submit/tizen_3.0-common/20170508.081301 submit/tizen_3.0-common/20170508.091535 submit/tizen_3.0/20170420.011011 submit/tizen_3.0_common/20170508.091735
authorSeungha Son <seungha.son@samsung.com>
Tue, 18 Apr 2017 00:54:18 +0000 (09:54 +0900)
committerSeungha Son <seungha.son@samsung.com>
Wed, 19 Apr 2017 23:12:25 +0000 (08:12 +0900)
Signed-off-by: Seungha Son <seungha.son@samsung.com>
Change-Id: I43af1f0eb21e3057e78b42528ed466c5d1780951
Signed-off-by: jusung son <jusung07.son@samsung.com>
src/notification_service.c

index 68f9267..fd42dc2 100755 (executable)
@@ -713,24 +713,29 @@ int notification_load_noti_by_tag(GVariant *parameters, GVariant **reply_body, u
                g_variant_get(parameters, "(&s&si)", &pkgname, &tag, &param_uid);
                ret = _validate_and_set_param_uid_with_uid(uid, &param_uid);
                if (ret != NOTIFICATION_ERROR_NONE)
-                       return ret;
+                       goto out;
 
                DbgPrint("_load_noti_by_tag pkgname : %s, tag : %s ", pkgname, tag);
                ret = notification_noti_get_by_tag(noti, pkgname, tag, param_uid);
+               if (ret != NOTIFICATION_ERROR_NONE) {
+                       ErrPrint("There is no notification that is matched tag [%s]", tag);
+                       goto out;
+               }
 
-               DbgPrint("notification_noti_get_by_tag ret : %d", ret);
                print_noti(noti);
-
                *reply_body = notification_ipc_make_gvariant_from_noti(noti, true);
-               notification_free(noti);
-
                if (*reply_body == NULL) {
                        ErrPrint("cannot make reply_body");
-                       return NOTIFICATION_ERROR_OUT_OF_MEMORY;
+                       ret = NOTIFICATION_ERROR_OUT_OF_MEMORY;
                }
        } else {
                ret = NOTIFICATION_ERROR_OUT_OF_MEMORY;
        }
+
+out:
+       if (noti)
+               notification_free(noti);
+
        DbgPrint("_load_noti_by_tag done !!");
        return ret;
 }
@@ -749,26 +754,27 @@ int notification_load_noti_by_priv_id(GVariant *parameters, GVariant **reply_bod
                g_variant_get(parameters, "(&sii)", &pkgname, &priv_id, &param_uid);
                ret = _validate_and_set_param_uid_with_uid(uid, &param_uid);
                if (ret != NOTIFICATION_ERROR_NONE)
-                       return ret;
+                       goto out;
 
                DbgPrint("load_noti_by_priv_id pkgname : %s, priv_id : %d ", pkgname, priv_id);
                ret = notification_noti_get_by_priv_id(noti, pkgname, priv_id, param_uid);
-
-               DbgPrint("notification_noti_get_by_priv_id ret : %d", ret);
-               print_noti(noti);
+               if (ret != NOTIFICATION_ERROR_NONE)
+                       goto out;
 
                *reply_body = notification_ipc_make_gvariant_from_noti(noti, true);
-               notification_free(noti);
-
                if (*reply_body == NULL) {
                        ErrPrint("cannot make reply_body");
-                       return NOTIFICATION_ERROR_OUT_OF_MEMORY;
+                       ret = NOTIFICATION_ERROR_OUT_OF_MEMORY;
                }
        } else {
                ret = NOTIFICATION_ERROR_OUT_OF_MEMORY;
        }
 
-       DbgPrint("_load_noti_by_priv_id done !!");
+out:
+       if (noti)
+               notification_free(noti);
+
+       DbgPrint("notification_load_noti_by_priv_id done [%d]", ret);
        return ret;
 }
 
@@ -1623,22 +1629,28 @@ int notification_add_noti_template(GVariant *parameters, GVariant **reply_body,
                g_variant_unref(body);
                if (ret != NOTIFICATION_ERROR_NONE) {
                        ErrPrint("failed to make a notification:%d\n", ret);
-                       return ret;
+                       goto out;
                }
 
                ret = notification_noti_check_count_for_template(noti, &count);
-               if (count >= NOTI_TEMPLATE_LIMIT)
-                       return NOTIFICATION_ERROR_MAX_EXCEEDED;
+               if (count >= NOTI_TEMPLATE_LIMIT) {
+                       ErrPrint("Exceed limit value that is saved tamplate :%d\n", ret);
+                       ret = NOTIFICATION_ERROR_MAX_EXCEEDED;
+                       goto out;
+               }
 
                ret = notification_noti_add_template(noti, template_name);
-               if (ret != NOTIFICATION_ERROR_NONE) {
+               if (ret != NOTIFICATION_ERROR_NONE)
                        ErrPrint("failed to add a notification:%d\n", ret);
-                       return ret;
-               }
        } else {
                ret = NOTIFICATION_ERROR_OUT_OF_MEMORY;
        }
 
+out:
+       if (noti)
+               notification_free(noti);
+
+       DbgPrint("notification_add_noti_template is done [%d]", ret);
        return ret;
 }
 
@@ -1656,27 +1668,30 @@ int notification_get_noti_template(GVariant *parameters, GVariant **reply_body,
                ret = aul_app_get_appid_bypid_for_uid(pid, appid, sizeof(appid), uid);
                if (ret != AUL_R_OK) {
                        ErrPrint("failed to get appid:%d", ret);
-                       return ret;
+                       ret = NOTIFICATION_ERROR_INVALID_PARAMETER;
+                       goto out;
                }
 
                ret = notification_noti_get_package_template(noti, appid, template_name);
                if (ret != NOTIFICATION_ERROR_NONE) {
                        DbgPrint("failed to get template:%d", ret);
-                       return ret;
+                       goto out;
                }
 
                *reply_body = notification_ipc_make_gvariant_from_noti(noti, false);
-               notification_free(noti);
-
                if (*reply_body == NULL) {
                        ErrPrint("cannot make reply_body");
-                       return NOTIFICATION_ERROR_OUT_OF_MEMORY;
+                       ret = NOTIFICATION_ERROR_OUT_OF_MEMORY;
                }
        } else {
                ret = NOTIFICATION_ERROR_OUT_OF_MEMORY;
        }
 
-       DbgPrint("_get_noti_template done !!");
+out:
+       if (noti)
+               notification_free(noti);
+
+       DbgPrint("notification_get_noti_template is done [%d]", ret);
        return ret;
 }
 
@@ -1693,23 +1708,24 @@ int notification_get_noti_package_template(GVariant *parameters, GVariant **repl
 
                ret = notification_noti_get_package_template(noti, pkgname, template_name);
                if (ret != NOTIFICATION_ERROR_NONE) {
-                       DbgPrint("failed to get template:%d", ret);
-                       return ret;
+                       ErrPrint("failed to get template:%d", ret);
+                       goto out;
                }
 
                *reply_body = notification_ipc_make_gvariant_from_noti(noti, false);
-               notification_free(noti);
-
                if (*reply_body == NULL) {
                        ErrPrint("cannot make reply_body");
-                       return NOTIFICATION_ERROR_OUT_OF_MEMORY;
+                       ret = NOTIFICATION_ERROR_OUT_OF_MEMORY;
                }
        } else {
                ret = NOTIFICATION_ERROR_OUT_OF_MEMORY;
        }
 
-       DbgPrint("_get_noti_package_template done !!");
+out:
+       if (noti)
+               notification_free(noti);
 
+       DbgPrint("notification_get_noti_package_template is done [%d]", ret);
        return ret;
 }
 
@@ -1861,53 +1877,60 @@ int notification_send_noti_event(GVariant *parameters, GVariant **reply_body)
        notification_h noti;
 
        noti = notification_create(NOTIFICATION_TYPE_NOTI);
-
        if (noti != NULL) {
                g_variant_get(parameters, "(vi)", &coupled_body, &event_type);
                g_variant_get(coupled_body, "(v)", &body);
 
                ret = notification_ipc_make_noti_from_gvariant(noti, body);
+               g_variant_unref(coupled_body);
+               g_variant_unref(body);
+
                if (ret != NOTIFICATION_ERROR_NONE) {
                        ErrPrint("failed to make a notification from gvariant");
-                       return ret;
+                       goto out;
                }
 
-               g_variant_unref(coupled_body);
-               g_variant_unref(body);
-
                ret = notification_get_id(noti, NULL, &priv_id);
                if (ret != NOTIFICATION_ERROR_NONE)
-                       return ret;
+                       goto out;
 
                info = __find_sender_info_by_priv_id(priv_id);
-               if (info == NULL || info->busname == NULL)
-                       return NOTIFICATION_ERROR_INVALID_PARAMETER;
+               if (info == NULL || info->busname == NULL) {
+                       ret = NOTIFICATION_ERROR_INVALID_PARAMETER;
+                       goto out;
+               }
 
                if (!info->watcher_id) {
                        if (!is_existed_busname(info->busname)) {
                                __event_list = g_list_remove(g_list_first(__event_list), info);
                                __free_event_info(info);
-                               return NOTIFICATION_ERROR_IO_ERROR;
+                               ret = NOTIFICATION_ERROR_IO_ERROR;
+                               goto out;
                        } else {
                                info->watcher_id = __insert_sender_watcher_id(info);
                        }
                }
 
                ret = send_event_notify_by_busname(parameters, "send_event", info->busname, PROVIDER_NOTI_EVENT_INTERFACE_NAME);
-               notification_free(noti);
+               if (ret != NOTIFICATION_ERROR_NONE) {
+                       ErrPrint("failed to send event");
+                       goto out;
+               }
 
+               *reply_body = g_variant_new("()");
+               if (*reply_body == NULL) {
+                       ErrPrint("cannot make reply body");
+                       ret = NOTIFICATION_ERROR_OUT_OF_MEMORY;
+               }
        } else {
                ret = NOTIFICATION_ERROR_OUT_OF_MEMORY;
        }
 
-       *reply_body = g_variant_new("()");
-       if (*reply_body == NULL) {
-               ErrPrint("cannot make reply body");
-               return NOTIFICATION_ERROR_OUT_OF_MEMORY;
-       }
-
-       DbgPrint("notification_send_noti_event done !! %d", ret);
+out:
+       if (noti)
+               notification_free(noti);
 
+       DbgPrint("notification_send_noti_event is done [%d]", ret);
        return ret;
 }
 
@@ -1979,9 +2002,7 @@ HAPI int notification_delete_noti_by_appid(const char *appid, uid_t uid)
        ret = notification_noti_delete_all(NOTIFICATION_TYPE_NONE, appid, &num_deleted, &list_deleted, uid);
        if (ret != NOTIFICATION_ERROR_NONE) {
                ErrPrint("failed to delete notifications:%d\n", ret);
-               if (list_deleted != NULL)
-                       free(list_deleted);
-               return ret;
+               goto out;
        }
 
        if (num_deleted > 0) {
@@ -1996,18 +2017,18 @@ HAPI int notification_delete_noti_by_appid(const char *appid, uid_t uid)
                g_variant_unref(deleted_noti_list);
                if (ret != NOTIFICATION_ERROR_NONE) {
                        ErrPrint("failed to send notify:%d\n", ret);
-                       return ret;
+                       goto out;
                }
 
                for (i = 0; i < num_deleted; i++) {
                        ret = __delete_sender_info(*(list_deleted + i));
                        if (ret != NOTIFICATION_ERROR_NONE)
-                               return ret;
+                               goto out;
                }
-
-               free(list_deleted);
        }
-
+out:
+       if (list_deleted != NULL)
+               free(list_deleted);
        return ret;
 }