Fix resource leak 44/183144/1
authorjusung son <jusung07.son@samsung.com>
Tue, 3 Jul 2018 01:43:18 +0000 (10:43 +0900)
committerjusung son <jusung07.son@samsung.com>
Tue, 3 Jul 2018 01:43:18 +0000 (10:43 +0900)
Change-Id: Ifb5051be6538e276e21444fef740df029f5ee724
Signed-off-by: jusung son <jusung07.son@samsung.com>
lib/src/shortcut_manager.c

index 6499489..12ff3ad 100755 (executable)
@@ -142,10 +142,10 @@ EAPI int shortcut_add_to_home(const char *name, shortcut_type type,
                const char *uri, const char *icon, int allow_duplicate,
                result_cb cb, void *data)
 {
-       struct result_cb_item *item;
-       char *appid;
+       struct result_cb_item *item = NULL;
+       char *appid = NULL;
        int ret;
-       GVariant *body;
+       GVariant *body = NULL;
        char *request_id = NULL;
 
        CHECK_SHORTCUT_FEATURE();
@@ -161,18 +161,16 @@ EAPI int shortcut_add_to_home(const char *name, shortcut_type type,
        if (ret != SHORTCUT_ERROR_NONE) {
                /* LCOV_EXCL_START */
                SHORTCUT_ERR("ready to send error [%d]", ret);
-               return ret;
+               goto out;
                /* LCOV_EXCL_STOP */
        }
 
        item = malloc(sizeof(struct result_cb_item));
        if (!item) {
                /* LCOV_EXCL_START */
-               if (appid)
-                       free(appid);
-
                SHORTCUT_ERR("Heap: %d\n", errno);
-               return SHORTCUT_ERROR_OUT_OF_MEMORY;
+               ret = SHORTCUT_ERROR_OUT_OF_MEMORY;
+               goto out;
                /* LCOV_EXCL_STOP */
        }
 
@@ -193,19 +191,16 @@ EAPI int shortcut_add_to_home(const char *name, shortcut_type type,
                        type, uri, icon, allow_duplicate);
 
        ret = _send_async_shortcut(body, item, "add_shortcut");
-       if (ret != SHORTCUT_ERROR_NONE) {
-               /* LCOV_EXCL_START */
-               free(item);
-               item = NULL;
-               /* LCOV_EXCL_STOP */
-       }
 
+out:
        if (appid)
                free(appid);
        if (body)
                g_variant_unref(body);
        if (request_id)
                free(request_id);
+       if (ret != SHORTCUT_ERROR_NONE && item)
+               free(item);
 
        return ret;
 }
@@ -214,10 +209,10 @@ EAPI int shortcut_add_to_home_widget(const char *name, shortcut_widget_size_e si
                const char *widget_id, const char *icon, double period,
                int allow_duplicate, result_cb cb, void *data)
 {
-       struct result_cb_item *item;
-       char *appid;
+       struct result_cb_item *item = NULL;
+       char *appid = NULL;
        int ret;
-       GVariant *body;
+       GVariant *body = NULL;
        char *request_id = NULL;
 
        CHECK_SHORTCUT_FEATURE();
@@ -238,18 +233,16 @@ EAPI int shortcut_add_to_home_widget(const char *name, shortcut_widget_size_e si
        if (ret != SHORTCUT_ERROR_NONE) {
                /* LCOV_EXCL_START */
                SHORTCUT_ERR("ready to send error [%d]", ret);
-               return ret;
+               goto out;
                /* LCOV_EXCL_STOP */
        }
 
        item = malloc(sizeof(struct result_cb_item));
        if (!item) {
                /* LCOV_EXCL_START */
-               if (appid)
-                       free(appid);
-
                SHORTCUT_ERR("Heap: %d\n", errno);
-               return SHORTCUT_ERROR_OUT_OF_MEMORY;
+               ret = SHORTCUT_ERROR_OUT_OF_MEMORY;
+               goto out;
                /* LCOV_EXCL_STOP */
        }
 
@@ -262,29 +255,25 @@ EAPI int shortcut_add_to_home_widget(const char *name, shortcut_widget_size_e si
 
        ret = _send_async_shortcut(body, item, "add_shortcut_widget");
 
-       if (ret != SHORTCUT_ERROR_NONE) {
-               /* LCOV_EXCL_START */
-               free(item);
-               item = NULL;
-               /* LCOV_EXCL_STOP */
-       }
-
+out:
        if (appid)
                free(appid);
        if (body)
                g_variant_unref(body);
        if (request_id)
                free(request_id);
+       if (ret != SHORTCUT_ERROR_NONE && item)
+               free(item);
 
        return ret;
 }
 
 EAPI int shortcut_remove_from_home(const char *name, result_cb cb, void *user_data)
 {
-       struct result_cb_item *item;
-       char *appid;
+       struct result_cb_item *item = NULL;
+       char *appid = NULL;
        int ret;
-       GVariant *body;
+       GVariant *body = NULL;
        char *request_id = NULL;
 
        CHECK_SHORTCUT_FEATURE();
@@ -298,18 +287,16 @@ EAPI int shortcut_remove_from_home(const char *name, result_cb cb, void *user_da
        if (ret != SHORTCUT_ERROR_NONE) {
                /* LCOV_EXCL_START */
                SHORTCUT_ERR("ready to send error [%d]", ret);
-               return ret;
+               goto out;
                /* LCOV_EXCL_STOP */
        }
 
        item = malloc(sizeof(struct result_cb_item));
        if (!item) {
                /* LCOV_EXCL_START */
-               if (appid)
-                       free(appid);
-
                SHORTCUT_ERR("Heap: %d\n", errno);
-               return SHORTCUT_ERROR_OUT_OF_MEMORY;
+               ret = SHORTCUT_ERROR_OUT_OF_MEMORY;
+               goto out;
                /* LCOV_EXCL_STOP */
        }
 
@@ -320,20 +307,16 @@ EAPI int shortcut_remove_from_home(const char *name, result_cb cb, void *user_da
        body = g_variant_new("(siss)", request_id, getpid(), appid, name);
 
        ret = _send_async_shortcut(body, item, "remove_shortcut");
-       if (ret != SHORTCUT_ERROR_NONE) {
-               /* LCOV_EXCL_START */
-               free(item);
-               item = NULL;
-               /* LCOV_EXCL_STOP */
-       }
 
+out:
        if (appid)
                free(appid);
        if (body)
                g_variant_unref(body);
        if (request_id)
                free(request_id);
-
+       if (ret != SHORTCUT_ERROR_NONE && item)
+               free(item);
        return ret;
 }