resourced-cpu-boosting: Check return value of funcs of resource-monitor 67/298767/4 accepted/tizen/8.0/unified/20231005.093834 accepted/tizen/unified/20230914.164939 tizen_8.0_m2_release
authorUnsung Lee <unsung.lee@samsung.com>
Wed, 13 Sep 2023 02:17:11 +0000 (11:17 +0900)
committerUnsung Lee <unsung.lee@samsung.com>
Thu, 14 Sep 2023 02:05:47 +0000 (11:05 +0900)
Check return value of functions of resource-monitor
to avoid passing negative value to a parameter that cannot be negative.

It solves the issue reported by Coverity with cid = 1708157

Change-Id: I975c3e9c31dd0240604ad64878cd21e80b2f4c4f
Signed-off-by: Unsung Lee <unsung.lee@samsung.com>
src/resourced-cpu-boosting/resourced-cpu-boosting.c

index 91de0936be7f64f26a52802fac214ba3a6e5f798..28f16363ac83167211b43a4436268aca2906d33a 100644 (file)
@@ -62,8 +62,8 @@ static syscommon_plugin_backend_resourced_cpu_boosting_funcs g_cpu_boosting_func
 #define PATH_BUF_MAX           256
 #define NAME_BUF_MAX           34
 
-int g_monitor_id;
-int g_system_resource_id;
+int g_monitor_id = 0;
+int g_system_resource_id = 0;
 u_int64_t g_system_attr_mask;
 
 static bool is_in_cgroup_tasks(pid_t tid, const char *path)
@@ -363,21 +363,44 @@ error:
 
 static int resourced_cpu_boosting_init(void **data)
 {
-       *data = (void *)&g_cpu_boosting_funcs;
-
+       /**
+        * TODO: If monitroing of system cpu util from resource-monitor
+        * failed due to some reasons, then replace monitoring source
+        * from resource-monitor to another.
+        */
        g_monitor_id = resource_monitor_init();
+       if (g_monitor_id < 0) {
+               *data = NULL;
+               return g_monitor_id;
+       }
+
        g_system_resource_id = resource_monitor_create_resource(g_monitor_id,
                        RESOURCE_MONITOR_TYPE_SYSTEM);
+       if (g_system_resource_id < 0) {
+               *data = NULL;
+               resource_monitor_exit(g_monitor_id);
+               g_monitor_id = 0;
+               return g_system_resource_id;
+       }
+
        g_system_attr_mask = (RESOURCE_MONITOR_SYSTEM_ATTR_CPU_UTIL);
 
        resource_monitor_set_resource_attr(g_monitor_id, g_system_resource_id,
                        g_system_attr_mask);
 
+       *data = (void *)&g_cpu_boosting_funcs;
+
        return 0;
 }
 
 static int resourced_cpu_boosting_exit(void *data)
 {
+       if (g_system_resource_id > 0)
+               resource_monitor_delete_resource(g_monitor_id, g_system_resource_id);
+
+       if (g_monitor_id > 0)
+               resource_monitor_exit(g_monitor_id);
+
        return 0;
 }