Prevent memory leak 48/230948/3
authorInHong Han <inhong1.han@samsung.com>
Tue, 14 Apr 2020 04:18:52 +0000 (13:18 +0900)
committerInHong Han <inhong1.han@samsung.com>
Thu, 16 Apr 2020 08:52:43 +0000 (17:52 +0900)
Change-Id: I89922a5fbe23851036c8633d285a9ca473ad31aa

server/stickerd_data_manager.c

index 23c09e0..2bb0012 100644 (file)
@@ -619,6 +619,49 @@ static void _send_sticker_changed_event(STICKER_EVENT_TYPE type, sticker_info_db
         g_variant_unref(body);
 }
 
+static void _free_sticker_data(sticker_info_db *sticker_data)
+{
+    if (!sticker_data)
+        return;
+
+    if (sticker_data->app_id) {
+        free(sticker_data->app_id);
+        sticker_data->app_id = NULL;
+    }
+
+    if (sticker_data->uri) {
+        free(sticker_data->uri);
+        sticker_data->uri = NULL;
+    }
+
+    if (sticker_data->thumbnail) {
+        free(sticker_data->thumbnail);
+        sticker_data->thumbnail = NULL;
+    }
+
+    if (sticker_data->keyword) {
+        g_list_free_full(sticker_data->keyword, free);
+        sticker_data->keyword = NULL;
+    }
+
+    if (sticker_data->group) {
+        free(sticker_data->group);
+        sticker_data->group = NULL;
+    }
+
+    if (sticker_data->description) {
+        free(sticker_data->description);
+        sticker_data->description = NULL;
+    }
+
+    if (sticker_data->date) {
+        free(sticker_data->date);
+        sticker_data->date = NULL;
+    }
+
+    free(sticker_data);
+}
+
 int stickerd_insert_sticker_info(GVariant *parameters, GVariant **reply_body)
 {
     int ret;
@@ -825,7 +868,7 @@ int stickerd_insert_sticker_info_by_json(GVariant *parameters, GVariant **reply_
                 continue;
             }
 
-            sticker_info->app_id = app_id;
+            sticker_info->app_id = strdup(app_id);
             sticker_info->type = _get_int_from_object(info_object, "type");
             if (sticker_info->type < 1)
                 goto free_memory;
@@ -880,7 +923,7 @@ int stickerd_insert_sticker_info_by_json(GVariant *parameters, GVariant **reply_
                 _send_sticker_changed_event(STICKER_EVENT_TYPE_INSERT, sticker_info);
 
 free_memory:
-            free(sticker_info);
+            _free_sticker_data(sticker_info);
             sticker_info = NULL;
         }
     }
@@ -923,13 +966,15 @@ int stickerd_del_sticker_info(GVariant *parameters, GVariant **reply_body)
     ret = stickerd_db_delete_sticker_info(record_id);
     if (ret != STICKERD_SERVER_ERROR_NONE) {
         LOGE("Failed to delete sticker info");
-        return STICKERD_SERVER_ERROR_OPERATION_FAILED;
+        ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
     } else {
-        if (sticker_info && sticker_info->uri) {
+        if (sticker_info && sticker_info->uri)
             _send_sticker_changed_event(STICKER_EVENT_TYPE_DELETE, sticker_info);
-            free(sticker_info);
-            sticker_info = NULL;
-        }
+    }
+
+    if (sticker_info) {
+        _free_sticker_data(sticker_info);
+        sticker_info = NULL;
     }
 
     return ret;
@@ -955,13 +1000,15 @@ int stickerd_del_sticker_info_by_uri(GVariant *parameters, GVariant **reply_body
     ret = stickerd_db_delete_sticker_info_by_uri(uri);
     if (ret != STICKERD_SERVER_ERROR_NONE) {
         LOGE("Failed to delete sticker info");
-        return STICKERD_SERVER_ERROR_OPERATION_FAILED;
+        ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
     } else {
-        if (sticker_info && sticker_info->uri) {
+        if (sticker_info && sticker_info->uri)
             _send_sticker_changed_event(STICKER_EVENT_TYPE_DELETE, sticker_info);
-            free(sticker_info);
-            sticker_info = NULL;
-        }
+    }
+
+    if (sticker_info) {
+        _free_sticker_data(sticker_info);
+        sticker_info = NULL;
     }
 
     return ret;
@@ -1159,29 +1206,7 @@ int stickerd_get_sticker_info(GVariant *parameters, GVariant **reply_body)
     ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
     if (ret != STICKERD_SERVER_ERROR_NONE) {
         LOGE("Failed to get sticker info");
-
-        if (sticker_info->app_id)
-            free(sticker_info->app_id);
-
-        if (sticker_info->uri)
-            free(sticker_info->uri);
-
-        if (sticker_info->thumbnail)
-            free(sticker_info->thumbnail);
-
-        if (sticker_info->keyword)
-            free(sticker_info->keyword);
-
-        if (sticker_info->group)
-            free(sticker_info->group);
-
-        if (sticker_info->description)
-            free(sticker_info->description);
-
-        if (sticker_info->date)
-            free(sticker_info->date);
-
-        free(sticker_info);
+        _free_sticker_data(sticker_info);
         sticker_info = NULL;
         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
     }
@@ -1205,13 +1230,14 @@ int stickerd_get_sticker_info(GVariant *parameters, GVariant **reply_body)
 
     if (*reply_body == NULL) {
         LOGE("Failed to create reply_body");
-        free(sticker_info);
+        _free_sticker_data(sticker_info);
         sticker_info = NULL;
         return STICKERD_SERVER_ERROR_OPERATION_FAILED;
     }
 
-    free(sticker_info);
+    _free_sticker_data(sticker_info);
     sticker_info = NULL;
+
     return ret;
 }
 
@@ -1739,11 +1765,11 @@ int stickerd_send_update_event(GVariant *parameters, GVariant **reply_body)
     sticker_info_db *sticker_info = (sticker_info_db *)calloc(1, sizeof(sticker_info_db));
     if (sticker_info) {
         ret = stickerd_db_get_sticker_info_by_record_id(record_id, sticker_info);
-        if (ret == STICKERD_SERVER_ERROR_NONE) {
+        if (ret == STICKERD_SERVER_ERROR_NONE)
             _send_sticker_changed_event(STICKER_EVENT_TYPE_UPDATE, sticker_info);
-            free(sticker_info);
-            sticker_info = NULL;
-        }
+
+        _free_sticker_data(sticker_info);
+        sticker_info = NULL;
     }
 
     return ret;