From 094f89dcd1555fb18ec1ff49c3503fa67f02936f Mon Sep 17 00:00:00 2001 From: Yunhee Seo Date: Mon, 16 Jan 2023 18:57:51 +0900 Subject: [PATCH] input: clean-up the reported warning by static analysis 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 --- src/input/input-dbus.c | 2 +- src/input/input-device-manager.c | 12 ++++++++++-- src/input/input-parser.c | 6 +++--- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/input/input-dbus.c b/src/input/input-dbus.c index df7ccd7..750ec76 100644 --- a/src/input/input-dbus.c +++ b/src/input/input-dbus.c @@ -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); diff --git a/src/input/input-device-manager.c b/src/input/input-device-manager.c index 3bd966a..8b28805 100644 --- a/src/input/input-device-manager.c +++ b/src/input/input-device-manager.c @@ -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; diff --git a/src/input/input-parser.c b/src/input/input-parser.c index ea45fdb..cb9fe90 100644 --- a/src/input/input-parser.c +++ b/src/input/input-parser.c @@ -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; -- 2.7.4