Fix bugs reported by Coverity and SVACE 33/277933/4
authorUnsung Lee <unsung.lee@samsung.com>
Fri, 15 Jul 2022 06:37:39 +0000 (15:37 +0900)
committerHyotaek Shim <hyotaek.shim@samsung.com>
Fri, 15 Jul 2022 06:54:20 +0000 (06:54 +0000)
Change-Id: I27303d7286a036a15171b023e083cfda4b043eb0
Signed-off-by: Unsung Lee <unsung.lee@samsung.com>
src/plugin/plugin.c

index 9cc68a5b64f931474da08bc5b90e63678e020bd6..87e4cb924650c26fb5bb83e56f5428dca06059e2 100644 (file)
@@ -41,7 +41,8 @@
 #endif
 #endif
 
-#define SOCK_PATH "/run/.resourced.socket"
+#define MAX_THREAD_NUM 131072                  /* This value is based on the max value of pid_max */
+#define SOCK_PATH              "/run/.resourced.socket"
 
 static int resource_create_and_connect_sock(void)
 {
@@ -154,7 +155,7 @@ API int resource_set_cpu_boosting (resource_pid_t pid,
        int sock;
        cpu_boosting_input_t input;
 
-       if (!resource_pid_input_is_valid(input.pid))
+       if (!resource_pid_input_is_valid(pid))
                return -1;
 
        if (!resource_cpu_boosting_level_input_is_valid(level))
@@ -180,7 +181,7 @@ API int resource_clear_cpu_boosting (resource_pid_t pid)
        int sock;
        cpu_boosting_input_t input;
 
-       if (!resource_pid_input_is_valid(input.pid))
+       if (!resource_pid_input_is_valid(pid))
                return -1;
 
        if ((sock = resource_create_and_connect_sock()) < 0)
@@ -215,6 +216,8 @@ API int resource_get_cpu_boosting_level (resource_pid_t pid,
 
        input.command = CPU_BOOSTING_COMMAND_GET;
        input.pid = pid;
+       input.level = CPU_BOOSTING_LEVEL_NONE;
+       input.timeout_msec = 0;
 
        ret = resource_cpu_boosting_send_command(input, sock);
        if (ret < 0)
@@ -224,6 +227,7 @@ API int resource_get_cpu_boosting_level (resource_pid_t pid,
        tv.tv_sec = 3;
        setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv, sizeof(tv));
 
+       memset(&output, 0, sizeof(output));
 retry_header:
        byte = recv(sock, (void *)&output, sizeof(output), 0);
        if (byte != sizeof(output)) {
@@ -245,7 +249,7 @@ retry_header:
                goto close_sock;
        }
 
-       if (output.level.tid_count > 0) {
+       if (output.level.tid_count > 0 && output.level.tid_count < MAX_THREAD_NUM) {
                level->tid_level = (int *)calloc(output.level.tid_count, sizeof(int));
                if (level->tid_level == NULL) {
                        _E("[CPU-BOOSTING-PLUGIN] Failed to allocate memory");
@@ -277,12 +281,17 @@ retry_body:
                        ret = -1;
                        goto close_sock;
                }
-       }
 
-       if (!output.success) {
-               _E("[CPU-BOOSTING-PLUGIN] Failed to get boosting");
-               free(level->tid_level);
+               if (!output.success) {
+                       _E("[CPU-BOOSTING-PLUGIN] Failed to get boosting from the server");
+                       free(level->tid_level);
+                       ret = -1;
+               }
+       }
+       else {
+               _E("[CPU-BOOSTING-PLUGIN] Returned tid_count is out of scope");
                ret = -1;
+               goto close_sock;
        }
 
 close_sock: