From: Unsung Lee Date: Fri, 15 Jul 2022 06:37:39 +0000 (+0900) Subject: Fix bugs reported by Coverity and SVACE X-Git-Tag: submit/tizen_6.5/20220722.040839~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7577a20161bebfdee6e8a0bcee67a9a18e5df5db;p=platform%2Fcore%2Fapi%2Fresource.git Fix bugs reported by Coverity and SVACE Change-Id: I27303d7286a036a15171b023e083cfda4b043eb0 Signed-off-by: Unsung Lee (cherry picked from commit 93605f8281f061022f241958d1ff6558a86b1e8a) --- diff --git a/src/plugin/plugin.c b/src/plugin/plugin.c index 9cc68a5..87e4cb9 100644 --- a/src/plugin/plugin.c +++ b/src/plugin/plugin.c @@ -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: