From 109a5c07c68eccd2206beed307bc5839159915bf Mon Sep 17 00:00:00 2001 From: "hwajeong.son" Date: Mon, 1 Oct 2018 19:54:55 +0900 Subject: [PATCH] Fixed coverity(prevent) issue --- src/adaptor.c | 2 + src/input_file.c | 166 ++++++++++++++++++++++++++--------------------- 2 files changed, 94 insertions(+), 74 deletions(-) diff --git a/src/adaptor.c b/src/adaptor.c index e52d02c..1e31a99 100755 --- a/src/adaptor.c +++ b/src/adaptor.c @@ -68,6 +68,8 @@ gboolean setup_cb(gpointer data) sa_inputfile_clear_completion_flag(); sa_inputfile_remove(); } + + return TRUE; } static GSourceFuncs SourceFuncs = { diff --git a/src/input_file.c b/src/input_file.c index 213bf00..83c19d7 100755 --- a/src/input_file.c +++ b/src/input_file.c @@ -30,15 +30,17 @@ #define CONFIG_FILE "/etc/setup-adaptor/config.json" #define CONFIG_FOLDER "/etc/setup-adaptor" #define CONFIG_NAME "config.json" +#define EVENT_NAME_MAX 256 #define EVENT_SIZE (sizeof(struct inotify_event)) -#define BUF_LEN (1024 * (EVENT_SIZE + 16)) +#define EVENT_BUF_LEN (512 * (EVENT_SIZE + EVENT_NAME_MAX)) static void *__config_main_loop(void *arg) { - int fd; - int wd; + int fd = 0; + int wd = 0; + int i = 0; file_state_cb callback; - char buffer[BUF_LEN]; + char *buffer = NULL; _D("__config_main_loop start\n"); @@ -52,60 +54,76 @@ static void *__config_main_loop(void *arg) fd = inotify_init(); if (fd < 0) { - _D("inotify_init error"); + _E("inotify_init error"); return NULL; } wd = inotify_add_watch(fd, CONFIG_FOLDER, IN_MODIFY | IN_CREATE | IN_DELETE); - // Start callack - _D("Registerd Callback Triggered"); - callback(SA_FILE_STATE_REGISTERED, NULL, NULL); - while (1) { - int length, i = 0; - length = read(fd, buffer, BUF_LEN); - - if (length < 0) - perror("read"); - - while (i < length) { - struct inotify_event *event = (struct inotify_event *)&buffer[i]; - _D("[debug] wd=%d mask=%d cookie=%d len=%d dir=%s", event->wd, event->mask, event->cookie, event->len, (event->mask & IN_ISDIR) ? "yes" : "no"); - if (event->len) { - if (event->mask & IN_CREATE) { - if (event->mask & IN_ISDIR) { - _D("The directory %s was created", event->name); - } else { - _D("The file %s was create.", event->name); - if (!strcmp(event->name, CONFIG_NAME)) - _D("config.json is created!!"); - } - } else if (event->mask & IN_DELETE) { - if (event->mask & IN_ISDIR) { - _D("The directory %s was deleted.", event->name); - } else { - _D("The file %s was deleted", event->name); - if (!strcmp(event->name, CONFIG_NAME)) - _D("config.json is deleted!!"); - } - } else if (event->mask & IN_MODIFY) { - if (event->mask & IN_ISDIR) { - _D("The directory %s was modified", event->name); - } else { - if (!strcmp(event->name, CONFIG_NAME)) { - _D("config.json is modified!!"); - callback(SA_FILE_STATE_CHANGED, NULL, NULL); + + if (wd < 0) { + _E("inotify_add_watch fail"); + close(fd); + return NULL; + } + + buffer = (char *) malloc(EVENT_BUF_LEN); + + if (buffer != NULL) { + // Start callack + _D("Registerd Callback Triggered"); + callback(SA_FILE_STATE_REGISTERED, NULL, NULL); + while (1) { + int length = 0; + length = read(fd, buffer, EVENT_BUF_LEN); + + if (length <= 0) { + _E("read error"); + break; + } + + i = 0; + while (i < length && i < (EVENT_BUF_LEN-EVENT_SIZE)) { + struct inotify_event *event = (struct inotify_event *)&buffer[i]; + if (event->len > 0 && event->len < EVENT_NAME_MAX) { + if (event->mask & IN_CREATE) { + if (event->mask & IN_ISDIR) { + _D("The directory was created"); + } else { + if (!strcmp(event->name, CONFIG_NAME)) + _D("config.json is created!!"); + } + } else if (event->mask & IN_DELETE) { + if (event->mask & IN_ISDIR) { + _D("The directory was deleted."); + } else { + if (!strcmp(event->name, CONFIG_NAME)) + _D("config.json is deleted!!"); + } + } else if (event->mask & IN_MODIFY) { + if (event->mask & IN_ISDIR) { + _D("The directory was modified"); + } else { + if (!strcmp(event->name, CONFIG_NAME)) + callback(SA_FILE_STATE_CHANGED, NULL, NULL); } } + i += EVENT_SIZE + event->len; + } else { + _E("event->len is wrong"); + break; } } - - i += EVENT_SIZE + event->len; } + } else { + _E("buffer is NULL"); } inotify_rm_watch(fd, wd); close(fd); + if (buffer != NULL) + free(buffer); + return NULL; } @@ -487,47 +505,47 @@ static int __parse_config(char *file, sa_config_s * setupConfig) int ret = 0; fd = open(file, O_RDONLY); - if (fd) { - len = lseek(fd, 0L, SEEK_END); + if (fd < 0) { + _E("config file can't be opened"); + return -1; + } - if (len > 0) { - lseek(fd, 0L, SEEK_SET); - jsonData = (char *)malloc(len + 1); - if (jsonData != NULL) { - memset(jsonData, 0x00, len + 1); - readLen = read(fd, jsonData, len); - _D("JSON full data[%s]", jsonData); + len = lseek(fd, 0L, SEEK_END); - configObj = json_tokener_parse(jsonData); + if (len > 0) { + lseek(fd, 0L, SEEK_SET); + jsonData = (char *)malloc(len + 1); + if (jsonData != NULL) { + memset(jsonData, 0x00, len + 1); + readLen = read(fd, jsonData, len); + _D("JSON full data[%s]", jsonData); - if (configObj != NULL && readLen > 0) { - // parse version - ret = __parse_version(configObj, setupConfig); - if (ret == 0) - ret = __parse_system_data(configObj, setupConfig); + configObj = json_tokener_parse(jsonData); - if (ret == 0) - ret = __parse_network_data(configObj, setupConfig); + if (configObj != NULL && readLen > 0) { + // parse version + ret = __parse_version(configObj, setupConfig); + if (ret == 0) + ret = __parse_system_data(configObj, setupConfig); - } else { - ret = -1; - _D("ConfigObj is not existed"); - } + if (ret == 0) + ret = __parse_network_data(configObj, setupConfig); - json_object_put(configObj); - free(jsonData); } else { ret = -1; - _D("memory allocation fail for jsonData"); + _D("ConfigObj is not existed"); } - } - close(fd); - } else { - ret = -1; - _D("config file can't be opened"); + json_object_put(configObj); + free(jsonData); + } else { + ret = -1; + _D("memory allocation fail for jsonData"); + } } + close(fd); + return ret; } @@ -659,7 +677,7 @@ void sa_inputfile_clear_completion_flag(void) void sa_inputfile_set_completion_flag(sa_file_config_e config_type) { - int fd; + int fd = 0; char buff[256]; switch (config_type) { -- 2.34.1