From b772f01c490f3769e00b5cd2b20af4e97965547a Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Wed, 22 Jan 2020 17:13:13 +0900 Subject: [PATCH] Fix static anlysis issue Change-Id: I8672a904fae3b77f158b4955f0383b76465d468b Signed-off-by: Hwankyu Jhun --- src/launchpad.c | 111 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 88 insertions(+), 23 deletions(-) diff --git a/src/launchpad.c b/src/launchpad.c index 16dc627..e751fbb 100755 --- a/src/launchpad.c +++ b/src/launchpad.c @@ -105,6 +105,8 @@ typedef struct { guint live_timer; int state; bool is_hydra; + guint pollfd; + guint hydra_pollfd; } candidate_process_context_t; typedef struct { @@ -2076,7 +2078,27 @@ end: return G_SOURCE_CONTINUE; } -static candidate_process_context_t *__add_slot(int type, int loader_id, +static void __destroy_slot(candidate_process_context_t *cpc) +{ + if (!cpc) + return; + + if (cpc->hydra_pollfd) + g_source_remove(cpc->hydra_pollfd); + + if (cpc->pollfd) + g_source_remove(cpc->pollfd); + + if (cpc->loader_extra) + free(cpc->loader_extra); + + if (cpc->loader_path) + free(cpc->loader_path); + + free(cpc); +} + +static candidate_process_context_t *__create_slot(int type, int loader_id, int caller_pid, const char *loader_path, const char *loader_extra, int detection_method, int activation_method, int deactivation_method, @@ -2085,16 +2107,27 @@ static candidate_process_context_t *__add_slot(int type, int loader_id, bool on_boot, bool app_exists, bool is_hydra) { candidate_process_context_t *cpc; - int fd = -1; - guint pollfd; - if (__find_slot(type, loader_id) != NULL) + cpc = calloc(1, sizeof(candidate_process_context_t)); + if (cpc == NULL) { + _E("Out of memory"); return NULL; + } - cpc = (candidate_process_context_t *)malloc( - sizeof(candidate_process_context_t)); - if (cpc == NULL) + cpc->loader_path = strdup(loader_path); + if (cpc->loader_path == NULL) { + _E("Failed to duplicate loader path(%s)", loader_path); + __destroy_slot(cpc); return NULL; + } + + cpc->loader_extra = loader_extra ? strdup(loader_extra) : strdup(""); + if (cpc->loader_extra == NULL) { + _E("Failed to duplicate loader extra(%s)", + loader_extra ? loader_extra : "null"); + __destroy_slot(cpc); + return NULL; + } cpc->type = type; cpc->prepared = false; @@ -2107,8 +2140,6 @@ static candidate_process_context_t *__add_slot(int type, int loader_id, cpc->last_exec_time = 0; cpc->source = 0; cpc->timer = 0; - cpc->loader_path = strdup(loader_path); - cpc->loader_extra = loader_extra ? strdup(loader_extra) : strdup(""); cpc->detection_method = detection_method; cpc->timeout_val = timeout_val; cpc->cpu_total_time = 0; @@ -2132,12 +2163,42 @@ static candidate_process_context_t *__add_slot(int type, int loader_id, else cpc->state = CANDIDATE_PROCESS_STATE_RUNNING; + return cpc; +} + +static candidate_process_context_t *__add_slot(int type, int loader_id, + int caller_pid, const char *loader_path, + const char *loader_extra, int detection_method, + int activation_method, int deactivation_method, + unsigned int ttl, int timeout_val, + int threshold_max, int threshold_min, + bool on_boot, bool app_exists, bool is_hydra) +{ + candidate_process_context_t *cpc; + int fd; + guint pollfd; + int hydra_fd; + guint hydra_pollfd; + + if (__find_slot(type, loader_id) != NULL) + return NULL; + + cpc = __create_slot(type, loader_id, + caller_pid, loader_path, + loader_extra, detection_method, + activation_method, deactivation_method, + ttl, timeout_val, + threshold_max, threshold_min, + on_boot, app_exists, is_hydra); + if (cpc == NULL) + return NULL; + fd = __listen_candidate_process(cpc->type, cpc->loader_id); if (fd == -1) { _E("[launchpad] Listening the socket to " \ "the type %d candidate process failed.", cpc->type); - free(cpc); + __destroy_slot(cpc); return NULL; } @@ -2145,29 +2206,37 @@ static candidate_process_context_t *__add_slot(int type, int loader_id, cpc->type, cpc->loader_id); if (pollfd == 0) { close(fd); - free(cpc); + __destroy_slot(cpc); return NULL; } + cpc->pollfd = pollfd; + if (is_hydra) { - fd = __listen_hydra_process(cpc->type, cpc->loader_id); - if (fd == -1) { + hydra_fd = __listen_hydra_process(cpc->type, cpc->loader_id); + if (hydra_fd == -1) { _E("[launchpad] Listening the socket to " \ "the type %d hydra process failed.", cpc->type); - free(cpc); + close(fd); + __destroy_slot(cpc); return NULL; } - pollfd = __poll_fd(fd, G_IO_IN, (GSourceFunc)__handle_hydra_event, - cpc->type, cpc->loader_id); - if (pollfd == 0) { + hydra_pollfd = __poll_fd(hydra_fd, G_IO_IN, + (GSourceFunc)__handle_hydra_event, + cpc->type, cpc->loader_id); + if (hydra_pollfd == 0) { + close(hydra_fd); close(fd); - free(cpc); + __destroy_slot(cpc); return NULL; } + + cpc->hydra_pollfd = hydra_pollfd; } + candidate_slot_list = g_list_append(candidate_slot_list, cpc); return cpc; @@ -2185,11 +2254,7 @@ static int __remove_slot(int type, int loader_id) __dispose_candidate_process(cpc); candidate_slot_list = g_list_delete_link( candidate_slot_list, iter); - free(cpc->loader_path); - if (cpc->loader_extra) - free(cpc->loader_extra); - - free(cpc); + __destroy_slot(cpc); return 0; } -- 2.7.4