Fix Svace Issue 03/61703/2
authorYoungHun Kim <yh8004.kim@samsung.com>
Thu, 10 Mar 2016 02:50:05 +0000 (11:50 +0900)
committerYoungHun Kim <yh8004.kim@samsung.com>
Sun, 13 Mar 2016 23:00:11 +0000 (16:00 -0700)
Change-Id: I6d9c41f0586d740cf98ecf44baae1d5a3446a127

include/muse_core_internal.h
src/muse_core.c
src/muse_core_config.c
src/muse_core_ipc.c
src/muse_core_log.c
src/muse_core_server.c

index 09e470c..f5645e4 100644 (file)
@@ -68,6 +68,7 @@ extern "C" {
 
 #define READ           0x02
 #define PERSIST        0x10
+#define MAX_ERROR_MSG_LEN      128
 
 #define DISPATCHER "dispatcher"
 #define CMD_DISPATCHER "cmd_dispatcher"
index 29f4990..1223a26 100644 (file)
@@ -51,12 +51,13 @@ static int _muse_core_free(muse_core_t *server);
 static int _muse_core_set_nonblocking(int fd, bool value)
 {
        int flags = fcntl(fd, F_GETFL, NULL);
+       char err_msg[MAX_ERROR_MSG_LEN] = {'\0',};
 
        if (flags >= 0) {
                flags = value ? (flags | O_NONBLOCK) : (flags & ~O_NONBLOCK);
 
                if (fcntl(fd, F_SETFL, flags) == -1) {
-                       LOGE("fcntl (%d, F_SETFL) %s", fd, strerror(errno));
+                       LOGE("fcntl (%d, F_SETFL) %s", fd, strerror_r(errno, err_msg, MAX_ERROR_MSG_LEN));
                        return -1;
                } else {
                        LOGD("fcntl (%d, F_SETFL)", fd);
@@ -119,8 +120,11 @@ static bool _muse_core_attach_server(int fd, muse_module_callback callback, gpoi
                return false;
 
        src = g_io_create_watch(channel, G_IO_IN);
-       if (!src)
+       if (!src) {
+               g_io_channel_unref(channel);
+               channel = NULL;
                return false;
+       }
 
        g_source_set_callback(src, (GSourceFunc) callback, param, NULL);
        g_source_attach(src, g_main_loop_get_context(g_loop));
@@ -188,6 +192,7 @@ int _muse_core_server_new(muse_core_channel_e channel)
        struct sockaddr *address;
        struct sockaddr_un addr_un;
        socklen_t address_len;
+       char err_msg[MAX_ERROR_MSG_LEN] = {'\0',};
 
        if (channel >= MUSE_CHANNEL_MAX)
                return -1;
@@ -197,7 +202,7 @@ int _muse_core_server_new(muse_core_channel_e channel)
        /* Create Socket */
        fd = socket(AF_UNIX, SOCK_STREAM, 0); /* Unix Domain Socket */
        if (fd < 0) {
-               LOGE("socket failed sock: %s", strerror(errno));
+               LOGE("socket failed sock: %s", strerror_r(errno, err_msg, MAX_ERROR_MSG_LEN));
                return -1;
        } else {
                LOGD("fd: %d", fd);
@@ -217,7 +222,7 @@ int _muse_core_server_new(muse_core_channel_e channel)
                }
 
                if (bind(fd, (struct sockaddr *)&addr_un, sizeof(addr_un)) != 0)
-                       LOGE("bind failed sock: %s", strerror(errno));
+                       LOGE("bind failed sock: %s", strerror_r(errno, err_msg, MAX_ERROR_MSG_LEN));
                close(fd);
                return -1;
        }
@@ -241,6 +246,7 @@ static gboolean _muse_core_connection_handler(GIOChannel *source, GIOCondition c
        socklen_t client_len;
        struct sockaddr_un client_address;
        muse_core_channel_e channel = (muse_core_channel_e)data;
+       char err_msg[MAX_ERROR_MSG_LEN] = {'\0',};
 
        LOGD("Enter");
 
@@ -256,7 +262,10 @@ static gboolean _muse_core_connection_handler(GIOChannel *source, GIOCondition c
 
        if (client_sockfd < 0) {
                LOGE("failed to accept");
-               return FALSE;
+               if (errno == EWOULDBLOCK || errno == ECONNABORTED)
+                       return FALSE;
+               else
+                       LOGE("accept: %s\n", strerror_r(errno, err_msg, MAX_ERROR_MSG_LEN));
        }
 
        if (channel == MUSE_CHANNEL_MSG) {
@@ -285,7 +294,8 @@ static gboolean _muse_core_connection_handler(GIOChannel *source, GIOCondition c
        LOGD("Leave");
        return TRUE;
 out:
-       close(client_sockfd);
+       if (client_sockfd)
+               close(client_sockfd);
        MUSE_FREE(module);
        MUSE_FREE(job);
 
@@ -298,6 +308,7 @@ static int _muse_core_client_new(muse_core_channel_e channel)
        struct sockaddr_un address;
        int len, ret = -1;
        int sockfd;
+       char err_msg[MAX_ERROR_MSG_LEN] = {'\0',};
 
        LOGD("Enter");
        if (channel >= MUSE_CHANNEL_MAX)
@@ -305,12 +316,12 @@ static int _muse_core_client_new(muse_core_channel_e channel)
 
        /*Create socket*/
        if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
-               LOGE("[socket failure] sock: %s", strerror(errno));
+               LOGE("[socket failure] sock: %s", strerror_r(errno, err_msg, MAX_ERROR_MSG_LEN));
                return ret;
        } else {
                LOGD("sockfd: %d", sockfd);
                if (fcntl(sockfd, F_SETFD, FD_CLOEXEC) < 0) {
-                       LOGE("unable to set on ctrls socket fd %d: %s", sockfd, strerror(errno));
+                       LOGE("unable to set on ctrls socket fd %d: %s", sockfd, strerror_r(errno, err_msg, MAX_ERROR_MSG_LEN));
                        (void) close(sockfd);
                        return -1;
                }
index b0de498..350222f 100644 (file)
@@ -39,6 +39,7 @@ static int _muse_core_config_parser(void)
        char *str;
        int idx;
        int ret = -1;
+       char *ptr = NULL;
 
        g_return_val_if_fail(g_muse_core_conf != NULL, ret);
 
@@ -85,7 +86,7 @@ static int _muse_core_config_parser(void)
        }
 
        g_muse_core_conf->type = 0;
-       host = strtok(g_muse_core_conf->hosts, COMMA);
+       host = strtok_r(g_muse_core_conf->hosts, COMMA, &ptr);
 
        while (host != NULL) {
                g_muse_core_conf->host[g_muse_core_conf->type] = strdup(host);
@@ -100,8 +101,8 @@ static int _muse_core_config_parser(void)
 
                /* path */
                strncpy(host_name, host, strlen(host) + 1);
-               strcat(host_name, COLON);
-               strcat(host_name, PATH);
+               strncat(host_name, COLON, strlen(COLON));
+               strncat(host_name, PATH, strlen(PATH));
                g_strstrip(host_name); /*Removes leading and trailing whitespace from a string*/
 
                g_muse_core_conf->host_infos[g_muse_core_conf->type] = (host_info_t *) malloc(sizeof(host_info_t));
@@ -124,8 +125,8 @@ static int _muse_core_config_parser(void)
 
                /* path */
                strncpy(host_name, host, strlen(host) + 1);
-               strcat(host_name, COLON);
-               strcat(host_name, PRELOADED);
+               strncat(host_name, COLON, strlen(COLON));
+               strncat(host_name, PRELOADED, strlen(PRELOADED));
                g_strstrip(host_name); /*Removes leading and trailing whitespace from a string*/
 
                g_muse_core_conf->host_infos[g_muse_core_conf->type]->preloaded= strdup(iniparser_getstring(g_muse_core_conf->muse_dict, host_name, NULL));
@@ -136,7 +137,7 @@ static int _muse_core_config_parser(void)
                        return ret;
                }
 
-               host = strtok(NULL, COMMA);
+               host = strtok_r(NULL, COMMA, &ptr);
                g_muse_core_conf->type++;
                MUSE_FREE(host_name);
        }
@@ -148,13 +149,14 @@ static void _muse_core_config_free(void)
 {
        char *host;
        int i = 0;
+       char *ptr = NULL;
 
        g_return_if_fail(g_muse_core_conf != NULL);
 
        if (g_muse_core_conf->muse_dict)
                iniparser_freedict(g_muse_core_conf->muse_dict);
 
-       host = strtok(g_muse_core_conf->hosts, COMMA);
+       host = strtok_r(g_muse_core_conf->hosts, COMMA, &ptr);
        g_muse_core_conf->type = 0;
 
        while (host != NULL) {
@@ -162,7 +164,7 @@ static void _muse_core_config_free(void)
                MUSE_FREE(g_muse_core_conf->host_infos[g_muse_core_conf->type]->path);
                MUSE_FREE(g_muse_core_conf->host_infos[g_muse_core_conf->type]->preloaded);
                MUSE_FREE(g_muse_core_conf->host_infos[g_muse_core_conf->type]);
-               host = strtok(NULL, COMMA);
+               host = strtok_r(NULL, COMMA, &ptr);
                g_muse_core_conf->type++;
        }
        MUSE_FREE(g_muse_core_conf->hosts);
index 4b93afa..8946e0c 100644 (file)
@@ -73,6 +73,7 @@ static gpointer _muse_core_ipc_dispatch_worker(gpointer data)
        int len, parse_len, cmd, api_module;
        muse_module_h module = NULL;
        muse_core_msg_parse_err_e err = MUSE_MSG_PARSE_ERROR_NONE;
+       char err_msg[MAX_ERROR_MSG_LEN] = {'\0',};
        g_return_val_if_fail(data != NULL, NULL);
 
        module = (muse_module_h)data;
@@ -82,7 +83,7 @@ static gpointer _muse_core_ipc_dispatch_worker(gpointer data)
                memset(module->recvMsg, 0x00, sizeof(module->recvMsg));
                len = muse_core_ipc_recv_msg(module->ch[MUSE_CHANNEL_MSG].fd, module->recvMsg);
                if (len <= 0) {
-                       LOGE("recv : %s (%d)", strerror(errno), errno);
+                       LOGE("recv : %s (%d)", strerror_r(errno, err_msg, MAX_ERROR_MSG_LEN), errno);
                        muse_core_cmd_dispatch(module, MUSE_MODULE_EVENT_SHUTDOWN);
                        _muse_core_ipc_client_cleanup(module);
                } else {
@@ -157,7 +158,7 @@ static gpointer _muse_core_ipc_data_worker(gpointer data)
        muse_module_h module = NULL;
        char *recvBuff = NULL;
        int allocSize = 0;
-
+       char err_msg[MAX_ERROR_MSG_LEN] = {'\0',};
        g_return_val_if_fail(fd > 0, NULL);
 
        while (1) {
@@ -173,7 +174,7 @@ static gpointer _muse_core_ipc_data_worker(gpointer data)
                currLen += recvLen;
                LOGD("buff %p, recvLen %d, currLen %d, allocSize %d", recvBuff, recvLen, currLen, allocSize);
                if (recvLen <= 0) {
-                       LOGE("[%d] recv : %s (%d)", fd, strerror(errno), errno);
+                       LOGE("[%d] recv : %s (%d)", fd, strerror_r(errno, err_msg, MAX_ERROR_MSG_LEN), errno);
                        break;
                } else {
                        if (module) {
@@ -359,11 +360,11 @@ int muse_core_ipc_send_msg(int sock_fd, const char *msg)
 int muse_core_ipc_recv_msg(int sock_fd, char *msg)
 {
        int ret = MM_ERROR_NONE;
-
+       char err_msg[MAX_ERROR_MSG_LEN] = {'\0',};
        g_return_val_if_fail(msg != NULL, MM_ERROR_INVALID_ARGUMENT);
 
        if ((ret = recv(sock_fd, msg, MUSE_MSG_MAX_LENGTH, 0)) < 0)
-               LOGE("fail to receive msg (%s)", strerror(errno));
+               LOGE("fail to receive msg (%s)", strerror_r(errno, err_msg, MAX_ERROR_MSG_LEN));
        else
                msg[ret] = '\0';
 
index 61fe9e4..15c0220 100644 (file)
@@ -52,8 +52,9 @@ static void _muse_core_log_flush_msg(void);
 
 static void _muse_core_log_sig_abort(int signo)
 {
+       char err_msg[MAX_ERROR_MSG_LEN] = {'\0',};
        if (SIG_ERR == signal(SIGABRT, SIG_DFL))
-               LOGE("SIGABRT handler: %s", strerror(errno));
+               LOGE("SIGABRT handler: %s", strerror_r(errno, err_msg, MAX_ERROR_MSG_LEN));
 
        static char client_name[256];
        memset(client_name, '\0', sizeof(client_name));
@@ -120,6 +121,7 @@ static void _muse_core_log_sigaction(int signo, siginfo_t *si, void *arg)
        int i;
        char **strings = NULL;
        ucontext_t *uctxt = NULL;
+       char err_msg[MAX_ERROR_MSG_LEN] = {'\0',};
 
        g_return_if_fail(si != NULL);
        g_return_if_fail(arg != NULL);
@@ -128,7 +130,7 @@ static void _muse_core_log_sigaction(int signo, siginfo_t *si, void *arg)
 
        tracesize = backtrace(trace, TUNABLE_CALLER_DEPTH);
        if (tracesize < 0)
-               LOGE("backtrace error: %s", strerror(errno));
+               LOGE("backtrace error: %s", strerror_r(errno, err_msg, MAX_ERROR_MSG_LEN));
 
        uctxt = (ucontext_t *) arg;
 
@@ -144,7 +146,7 @@ static void _muse_core_log_sigaction(int signo, siginfo_t *si, void *arg)
        #endif
        strings = backtrace_symbols(trace, tracesize);
        if (strings == NULL) {
-               LOGE("backtrace_symbols error: %s", strerror(errno));
+               LOGE("backtrace_symbols error: %s", strerror_r(errno, err_msg, MAX_ERROR_MSG_LEN));
        } else {
                /* skip the first stack frame because it just points here. */
                for (i = 1; i < tracesize; ++i) {
@@ -161,8 +163,6 @@ static void _muse_core_log_sigaction(int signo, siginfo_t *si, void *arg)
 
 static int _muse_core_log_open_work(const char *path)
 {
-       if (access(path, F_OK) == 0)
-               unlink(path);
        return open(path, O_CREAT | O_APPEND | O_WRONLY | O_NONBLOCK, 0666);
 }
 
@@ -180,11 +180,7 @@ static void _muse_core_log_create_fd(void)
                        stat(file[index], &st);
                        g_muse_core_log->size = st.st_size;
                        if (g_muse_core_log->size > MAX_SIZE) {
-                               if (index == MAX_FILE_NUM - 1)
-                                       selected_index = 0;
-                               else
-                                       selected_index = index + 1;
-
+                               selected_index = (index + 1) % MAX_FILE_NUM;
                                break;
                        } else {
                                selected_index = index;
@@ -209,12 +205,13 @@ static void _muse_core_log_create_fd(void)
 
 static void _muse_core_log_set_log_fd(void)
 {
+       char err_msg[MAX_ERROR_MSG_LEN] = {'\0',};
        g_return_if_fail(g_muse_core_log != NULL);
 
        _muse_core_log_create_fd();
 
        if (fcntl(g_muse_core_log->log_fd, F_SETFD, FD_CLOEXEC) < 0)
-               LOGE("unable to set CLO_EXEC on log fd %d: %s", g_muse_core_log->log_fd, strerror(errno));
+               LOGE("unable to set CLO_EXEC on log fd %d: %s", g_muse_core_log->log_fd, strerror_r(errno, err_msg, MAX_ERROR_MSG_LEN));
 
        (void) _muse_core_log_fd_set_block(g_muse_core_log->log_fd);
 }
index f85dd66..29b5fa1 100644 (file)
@@ -75,9 +75,8 @@ static void _muse_core_server_gst_init(char **cmd)
        }
 
        /* release */
-       for (i = 0; i < *argc; i++) {
+       for (i = 0; i <= *argc; i++)
                MUSE_FREE(argv[i]);
-       }
 
        MUSE_FREE(argv);
        MUSE_FREE(argc);