input: clean-up the reported warning by static analysis 31/286931/1 accepted/tizen/7.0/unified/20230118.093750
authorYunhee Seo <yuni.seo@samsung.com>
Mon, 16 Jan 2023 09:57:51 +0000 (18:57 +0900)
committeryunhee seo <yuni.seo@samsung.com>
Tue, 17 Jan 2023 04:44:17 +0000 (04:44 +0000)
improve code reported warning by static analysis
to avoid memory leak, build warning, unexpected return value issues

Change-Id: Ic9c3435c6515e645b52cdbc5feca223f918f6443
Signed-off-by: Yunhee Seo <yuni.seo@samsung.com>
(cherry picked from commit 094f89dcd1555fb18ec1ff49c3503fa67f02936f)

src/input/input-dbus.c
src/input/input-device-manager.c
src/input/input-parser.c

index df7ccd7..750ec76 100644 (file)
@@ -139,7 +139,7 @@ static GVariant *dbus_inputgetdevicename(GDBusConnection *conn,
 {
        GVariant *dbus_ret;
        int input_id, ret = 0;
-       char *input_device_name;
+       char *input_device_name = NULL;
        pid_t pid;
 
        g_variant_get(param, "(i)", &input_id);
index 3bd966a..8b28805 100644 (file)
@@ -23,7 +23,7 @@
 #include "shared/log.h"
 #include "input-device-manager.h"
 
-static struct input_device_element {
+struct input_device_element {
        int id;
        int device_default;
        char *name;
@@ -31,9 +31,16 @@ static struct input_device_element {
 
 static GHashTable *g_input_device_list;
 
+static void cleanup_input_device_element(gpointer data)
+{
+       struct input_device_element *input_device_elem = data;
+       free(input_device_elem->name);
+       free(input_device_elem);
+}
+
 static void destroy_input_device_hashtable_value(gpointer data)
 {
-       g_list_free((GList*)data);
+       g_list_free_full(data, cleanup_input_device_element);
 }
 
 int input_devman_init(void)
@@ -137,6 +144,7 @@ int input_devman_add_device(int input_device_type, int input_device_id,
        input_device_elem = malloc(sizeof(struct input_device_element));
        if (!input_device_elem) {
                _E("Failed to malloc input device element");
+               g_free(hash_key);
                return -ENOMEM;
        }
        input_device_elem->id = input_device_id;
index ea45fdb..cb9fe90 100644 (file)
@@ -32,7 +32,7 @@
 
 #define INPUT_CONF_PATH    "/hal/etc/deviced/input.conf"
 
-static struct input_device_info {
+struct input_device_info {
        int input_device_type;
        int input_device_id;
        char *input_device_name;
@@ -74,8 +74,8 @@ static void parse_input_device_property(gpointer data, gpointer user_data)
        } else if (MATCH(prop->key, "InputDeviceId")) {
                sscanf(prop->value, "%d", (&input_dev_info->input_device_id));
        } else if (MATCH(prop->key, "InputDeviceName")) {
-               input_dev_info->input_device_name = calloc(sizeof(prop->value), sizeof(char));
-               strcpy(input_dev_info->input_device_name, prop->value);
+               int str_len = sizeof(prop->value);
+               input_dev_info->input_device_name = strndup(prop->value, str_len);
        } else if (MATCH(prop->key, "InputDeviceDefault")) {
                if (MATCH(prop->value, "yes"))
                        input_dev_info->input_device_default = 1;