Fix static analysis issues 59/197759/2
authorHwankyu Jhun <h.jhun@samsung.com>
Wed, 16 Jan 2019 06:27:36 +0000 (15:27 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Wed, 16 Jan 2019 06:36:04 +0000 (15:36 +0900)
- Checks return values
- Checks the file descriptor

Change-Id: Id9bc94e63b44e959f8c1cfeb20965b44724fa592
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
inc/launchpad_common.h
src/launchpad.c
src/launchpad_common.c

index 3b789f5..4662d3d 100644 (file)
@@ -111,7 +111,7 @@ app_pkt_t *_recv_pkt_raw(int fd);
 app_pkt_t *_accept_recv_pkt_raw(int fd, int *clifd, struct ucred *cr);
 int _send_pkt_raw(int client_fd, app_pkt_t *pkt);
 int  _connect_to_launchpad(int type, int id);
-void _set_sock_option(int fd, int cli);
+int _set_sock_option(int fd, int cli);
 void _set_env(appinfo_t *menu_info, bundle *kb);
 char **_create_argc_argv(bundle *kb, int *margc);
 char *_get_libdir(const char *path);
index 3fa9a2f..3b2592a 100755 (executable)
@@ -432,7 +432,10 @@ static int __accept_candidate_process(int server_fd, int *out_client_fd,
                goto error;
        }
 
-       _set_sock_option(client_fd, 1);
+       if (_set_sock_option(client_fd, 1) < 0) {
+               _E("Failed to set sock option");
+               goto error;
+       }
 
        recv_ret = recv(client_fd, &client_pid, sizeof(client_pid),
                        MSG_WAITALL);
@@ -515,6 +518,11 @@ static int __candidate_process_real_launch(int candidate_fd, app_pkt_t *pkt)
 
 static int __real_send(int clifd, int ret)
 {
+       if (clifd < 3) {
+               _E("Invalid parameter. clifd(%d)", clifd);
+               return -1;
+       }
+
        if (send(clifd, &ret, sizeof(int), MSG_NOSIGNAL) < 0) {
                if (errno == EPIPE) {
                        _E("send failed due to EPIPE.");
index e1d22c8..da3ba04 100644 (file)
@@ -134,21 +134,47 @@ void _get_cpu_idle(unsigned long long *total, unsigned long long *idle)
        *idle = iv;
 }
 
-void _set_sock_option(int fd, int cli)
+int _set_sock_option(int fd, int cli)
 {
+       struct timeval tv = { 5, 200 * 1000 };  /*  5.2 sec */
        int size;
        int flag;
-       struct timeval tv = { 5, 200 * 1000 };  /*  5.2 sec */
+       int ret;
 
        size = AUL_SOCK_MAXBUFF;
-       setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
-       setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
+       ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
+       if (ret < 0) {
+               _E("Failed to set SO_SNDBUF option on socket. errno(%d)",
+                               errno);
+               return -1;
+       }
+
+       ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
+       if (ret < 0) {
+               _E("Failed to set SO_RCVBUF option on socket. errno(%d)",
+                               errno);
+               return -1;
+       }
+
        if (cli) {
-               setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
+               ret = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
+               if (ret < 0) {
+                       _E("Failed to set SO_RCVTIMEO option on socket. " \
+                                       "errno(%d)", errno);
+                       return -1;
+               }
+
                flag = fcntl(fd, F_GETFD);
                flag |= FD_CLOEXEC;
-               fcntl(fd, F_SETFD, flag);
+               ret = fcntl(fd, F_SETFD, flag);
+               if (ret < 0) {
+                       _E("Failed to manipulate fd(F_SETFD), errno(%d)",
+                                       errno);
+                       return -1;
+               }
        }
+
+       return 0;
 }
 
 static int __parse_app_path(const char *arg, char *out, int out_size)
@@ -322,7 +348,11 @@ int _create_server_sock(const char *name)
                return -1;
        }
 
-       _set_sock_option(fd, 0);
+       if (_set_sock_option(fd, 0) < 0) {
+               _E("Failed to set sock option");
+               close(fd);
+               return -1;
+       }
 
        if (listen(fd, 128) == -1) {
                _E("listen error");
@@ -413,7 +443,11 @@ app_pkt_t *_accept_recv_pkt_raw(int fd, int *clifd, struct ucred *cr)
                return NULL;
        }
 
-       _set_sock_option(newfd, 1);
+       if (_set_sock_option(newfd, 1) < 0) {
+               _E("Failed to set sock option");
+               close(newfd);
+               return NULL;
+       }
 
        pkt = _recv_pkt_raw(newfd);
        if (pkt == NULL) {
@@ -1175,7 +1209,11 @@ static int __create_app_socket(int pid, uid_t uid)
                return -1;
        }
 
-       _set_sock_option(fd, 0);
+       if (_set_sock_option(fd, 0) < 0) {
+               _E("Failed to set sock option");
+               close(fd);
+               return -1;
+       }
 
        if (listen(fd, 128) < 0) {
                _E("Failed to listen %d, errno(%d)", fd, errno);