input: Add a handling case for memory allocation failure 31/310431/3 accepted/tizen/7.0/unified/20240502.164113
authorYunhee Seo <yuni.seo@samsung.com>
Mon, 29 Apr 2024 02:35:08 +0000 (11:35 +0900)
committerYunhee Seo <yuni.seo@samsung.com>
Tue, 30 Apr 2024 05:23:09 +0000 (14:23 +0900)
While parsing input event conf file, memory callocaion can fails.
However, free handling code was omitted in the allocation failed case.
To prevent memory leak, this is necessary.

Change-Id: Ia8b5239675833a068234886c3031a84c8c081dd2
Signed-off-by: Yunhee Seo <yuni.seo@samsung.com>
plugins/iot-headless/input/input-config.c

index 1ee72c8..4597302 100644 (file)
@@ -706,7 +706,7 @@ static void parse_event_action_property(gpointer data, gpointer user_data)
 static int parse_event_action(const struct parse_result *result, void *data)
 {
        struct input_config *ic;
-       struct input_parse_event_action *input_event_action;
+       struct input_parse_event_action input_event_action;
        GHashTable *hash_table = (GHashTable *) data;
 
        if (!result || !result->props)
@@ -717,28 +717,30 @@ static int parse_event_action(const struct parse_result *result, void *data)
 
        _D("Input section=%s", result->section);
 
-       input_event_action = calloc(1, sizeof(struct input_parse_event_action));
-       if (!input_event_action)
-               return 0;
-
-       input_event_action->ieu = calloc(1, sizeof(struct input_event_unit));
-       if (!input_event_action->ieu)
-               return 0;
+       input_event_action.ieu = calloc(1, sizeof(struct input_event_unit));
+       if (!input_event_action.ieu)
+               goto fail_mem_alloc;
 
-       input_event_action->hash_table_key_infos = hash_table;
-       g_list_foreach(result->props, parse_event_action_property, input_event_action);
+       input_event_action.hash_table_key_infos = hash_table;
+       g_list_foreach(result->props, parse_event_action_property, &input_event_action);
 
-       ic = find_input_config(input_event_action->ieu->keycode);
+       ic = find_input_config(input_event_action.ieu->keycode);
        if (!ic) {
                ic = calloc(1, sizeof(struct input_config));
                if (!ic)
-                       return -ENOMEM;
-               ic->keycode = input_event_action->ieu->keycode;
+                       goto fail_mem_alloc;
+               ic->keycode = input_event_action.ieu->keycode;
                SYS_G_LIST_APPEND(input_config_list, ic);
        }
-       SYS_G_LIST_APPEND(ic->event_list, input_event_action->ieu);
+       SYS_G_LIST_APPEND(ic->event_list, input_event_action.ieu);
 
        return 0;
+
+fail_mem_alloc:
+       if (input_event_action.ieu)
+               free(input_event_action.ieu);
+
+       return -ENOMEM;
 }
 
 /* return 1 if the condition met, otherwise return 0 */