Fix file descriptor management 72/251172/1
authorHwankyu Jhun <h.jhun@samsung.com>
Mon, 11 Jan 2021 03:29:49 +0000 (12:29 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Mon, 11 Jan 2021 03:41:21 +0000 (12:41 +0900)
This patch removes double free and leakages.

Change-Id: Ic8bed9d6ea9efd409005a56eada078978b498461
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/lib/amd_app_status.c
src/lib/amd_launch.c
src/modules/widget/src/amd_widget.c

index bd943d1251a842f9aadadae76eec981a9df2bd57..6ec8df9855502cc55011507c71f0a43e1da29de7 100644 (file)
@@ -1125,8 +1125,8 @@ static int __send_running_appinfo(app_status_h app_status, int fd)
        if (b == NULL) {
                _E("out of memory");
                aul_sock_send_raw_with_fd(fd, APP_GET_INFO_ERROR,
-                               NULL, 0, AUL_SOCK_NOREPLY);
-               return -1;
+                               NULL, 0, AUL_SOCK_ASYNC);
+               return -ENOMEM;
        }
 
        ret = bundle_encode(b, &raw, &len);
@@ -1134,8 +1134,8 @@ static int __send_running_appinfo(app_status_h app_status, int fd)
        if (ret != BUNDLE_ERROR_NONE) {
                _E("Failed to encode bundle");
                aul_sock_send_raw_with_fd(fd, APP_GET_INFO_ERROR, NULL,
-                               0, AUL_SOCK_NOREPLY);
-               return -1;
+                               0, AUL_SOCK_ASYNC);
+               return -ENOMEM;
        }
 
        ret = aul_sock_send_raw_with_fd(fd, APP_GET_INFO_OK,
@@ -1191,8 +1191,9 @@ int _app_status_send_running_appinfo(int fd, int cmd, uid_t uid)
 
                ret = __send_running_appinfo(app_status, fd);
                if (ret < 0) {
+                       close(fd);
                        g_slist_free(list);
-                       return -1;
+                       return ret;
                }
        }
        close(fd);
@@ -2255,10 +2256,10 @@ static int __dispatch_app_context_get(request_h req)
        }
 
        ret = __send_running_appinfo(app_status, fd);
+       close(fd);
        if (ret < 0)
                return ret;
 
-       close(fd);
        _I("[APP_CONTEXT_GET] result: %d", ret);
        return 0;
 }
@@ -2322,10 +2323,10 @@ static int __dispatch_app_context_get_by_instance_id(request_h req)
        }
 
        ret = __send_running_appinfo(app_status, fd);
+       close(fd);
        if (ret < 0)
                return ret;
 
-       close(fd);
        _I("[APP_CONTEXT_GET_BY_INSTANCE_ID] result: %d", ret);
        return 0;
 }
@@ -2378,10 +2379,10 @@ static int __dispatch_app_context_get_by_pid(request_h req)
        }
 
        ret = __send_running_appinfo(app_status, fd);
+       close(fd);
        if (ret < 0)
                return ret;
 
-       close(fd);
        _I("[APP_CONTEXT_GET_BY_PID] result: %d", ret);
        return 0;
 }
@@ -2603,4 +2604,4 @@ int _app_status_finish(void)
 bool _app_status_is_debug_mode(app_status_h app_status)
 {
        return app_status->debug_mode;
-}
\ No newline at end of file
+}
index 9f315619a7991c50b07ad25970add2d93d7e7f4f..0dbc405e190366093495fabe06baa91264b2345a 100644 (file)
@@ -154,6 +154,8 @@ static void __destroy_result_info(gpointer data)
                g_source_remove(info->timer);
        if (info->seq)
                free(info->seq);
+       if (info->fd > 0)
+               close(info->fd);
        free(info);
 }
 
@@ -1389,6 +1391,7 @@ static void __pend_result_info(request_h req, int pid)
        const char *seq;
        struct timespec end;
        unsigned int interval;
+       int fd;
 
        if (pid <= 0)
                return;
@@ -1402,10 +1405,11 @@ static void __pend_result_info(request_h req, int pid)
 
        clock_gettime(CLOCK_MONOTONIC, &end);
        interval = __get_timeout_interval(_request_get_start_time(req), &end);
-       info = __create_result_info(interval, pid,
-                       _request_remove_fd(req), seq);
+       fd = _request_remove_fd(req);
+       info = __create_result_info(interval, pid, fd, seq);
        if (!info) {
                _E("Failed to create result info");
+               close(fd);
                return;
        }
 
@@ -1586,6 +1590,7 @@ static int __dispatch_app_result(request_h req)
        if (info) {
                res = aul_sock_send_bundle_with_fd(info->fd, pgid, kb,
                                AUL_SOCK_NOREPLY);
+               info->fd = -1;
                __remove_result_info(info);
                __destroy_result_info(info);
        } else {
index 7c740d54da99002b405a580eb4a35d10458ec2e2..140251ce4586794143baff63d7b65f90167d9c77 100644 (file)
@@ -1135,7 +1135,7 @@ static int __send_running_info(widget_t *widget, int fd)
        if (b == NULL) {
                LOGE("Failed to create bundle");
                aul_sock_send_raw_with_fd(fd, APP_GET_INFO_ERROR,
-                               NULL, 0, AUL_SOCK_NOREPLY);
+                               NULL, 0, AUL_SOCK_ASYNC);
                return -1;
        }
 
@@ -1156,7 +1156,7 @@ static int __send_running_info(widget_t *widget, int fd)
                if (b_raw == NULL) {
                        LOGE("Failed to encode bundle");
                        aul_sock_send_raw_with_fd(fd, APP_GET_INFO_ERROR,
-                                       NULL, 0, AUL_SOCK_NOREPLY);
+                                       NULL, 0, AUL_SOCK_ASYNC);
                        bundle_free(b);
                        return -1;
                }