hal-backend-service: Fix the usage of free functions of GString 16/321116/3
authorSangYoun Kwak <sy.kwak@samsung.com>
Fri, 14 Mar 2025 02:15:49 +0000 (11:15 +0900)
committerSangYoun Kwak <sy.kwak@samsung.com>
Fri, 14 Mar 2025 12:37:59 +0000 (21:37 +0900)
Previously, a glib function g_string_free to free GString object was
used like below:
(task_name is a GString object)
 1. Assign task_name->str into another char * type variable manually.
 2. Free task_name as g_string_free(task_name, FALSE);
    This will free GString object without its buffer.
    Buffer is provided as a return value but this buffer is already
    assigned to another variable, it is ignored.
 3. If error occurs, g_string_free(task_name, FALSE) is called.

This may cause memory leak in the error situation and even in the
non-error situation, ignoring the return value of a function call might
cause problem during maintainance.

Thus, it is fixed to get buffer from the return value of
g_string_free_and_steal, which frees GString object and returns buffer.
(It is equivalent to "g_string_free(task_name, FALSE)")
In the error situation, GString object is freed completely with its
buffer.

Change-Id: Idc1f0667819e67fa51823bfc37da523993a1b120
Signed-off-by: SangYoun Kwak <sy.kwak@samsung.com>
hal-backend-service/hal-backend-service.c

index 67f7b1684789809ee4bb2b06ca7f7a295343bd60..b665f00118ee4b16ea6aa2d1c50d61cf7a60ccc6 100644 (file)
@@ -717,11 +717,12 @@ static void create_hal_backend_service_task(gpointer data, gpointer user_data)
        }
 
        service_data->task = task;
-       service_data->task_name = task_name->str;
+       service_data->task_name = g_string_free_and_steal(task_name);
+       task_name = NULL;
 
-       _D("Task created: %s", task_name->str);
+       _D("Task created: %s", service_data->task_name);
 
-       goto free_resources;
+       return;
 
 error:
        if (source != NULL)
@@ -730,11 +731,10 @@ error:
        if (task != NULL)
                tizen_core_task_destroy(task);
 
-       (*failed_count)++;
-
-free_resources:
        if (task_name != NULL)
-               g_string_free(task_name, FALSE);
+               g_string_free(task_name, TRUE);
+
+       (*failed_count)++;
 
        return;
 }