Fix the svace and coverity issue 09/316209/3 accepted/tizen/unified/20240821.081505 accepted/tizen/unified/dev/20240822.230409 accepted/tizen/unified/x/20240822.014247
authorYoungHun Kim <yh8004.kim@samsung.com>
Fri, 16 Aug 2024 01:39:39 +0000 (10:39 +0900)
committerYoungHun Kim <yh8004.kim@samsung.com>
Mon, 19 Aug 2024 23:51:34 +0000 (08:51 +0900)
 - UNINIT.CTOR.MANY DEREF_OF_NULL.RET.ALLOC etc

Change-Id: I7c5149a98f3bc73fc36cb218f006015d7d7a0e54

14 files changed:
include/RMShm.h
include_internal/RMShmCache.h
packaging/resource-manager.spec
src/RMShmCache.cpp
src/rm_api.cpp
src/rm_callback.cpp
ut/testcase/TCCallbackListener.cpp
ut/testcase/TCCallbackListener.h
ut/testcase/TCPlayer.cpp
ut/testcase/TCPlayer.h
ut/testcase/TCResource.cpp
ut/testcase/TCResource.h
ut/testcase/ut_main.cpp
ut/testcase/ut_tc.cpp

index bc21e7d851dd4335ec70278daa72e3da28c811bd..eb38d72c9bc5a0e40961faf22afb679177510a19 100644 (file)
@@ -170,9 +170,9 @@ protected:
        void SetDeviceID(const int device_id) { device_id_ = device_id; }
 
 private:
-       int device_id_;
-       int state_;
-       int player_id_;
+       int device_id_ {};
+       int state_ {};
+       int player_id_ {};
 };
 
 /**
@@ -270,8 +270,8 @@ public:
        int GetVirtualID(void) { return virtual_id_; }
 
 private:
-       int zone_id_;
-       int virtual_id_;
+       int zone_id_ {};
+       int virtual_id_ {};
 };
 
 #ifdef __cplusplus
index 0d57ad92356c9a2df02f422542f664cd31da3a8f..f8c9a129a48daab3adcf29bbea5cc80255108abd 100644 (file)
@@ -55,7 +55,7 @@ private:
 
        bool Init(void);
 
-       bool ready_;
+       bool ready_ {};
        std::mutex init_mutex_;
 
        shared::segment *shm_segment_;
index 6f0df4c2eee630042587d75b0a92d8d3674a85a8..ec83557227e54a6cd35f2dcc37ead4136c2e9548 100644 (file)
@@ -1,7 +1,7 @@
 Name:       resource-manager
 Summary:    Resource manager
 Version:    0.1
-Release:    1
+Release:    2
 Group:      Multimedia/Libraries
 License:    Apache-2.0
 Source0:    %{name}-%{version}.tar.gz
index 36cb1809bade77b13b8a5a214bc5eaea47328029..ba3621f74c1cb4aa836dbfc59e7a302d1a1471e8 100644 (file)
@@ -27,27 +27,27 @@ static std::mutex instance_mutex_;
 
 RMShmCache *RMShmCache::GetInstance(void)
 {
-       if (instance_ == nullptr) {
-               std::lock_guard<std::mutex> guard(instance_mutex_);
-               if (instance_ == nullptr) {
-                       instance_ = new RMShmCache();
-               }
-       }
+       std::lock_guard<std::mutex> guard(instance_mutex_);
+
+       if (instance_ == nullptr)
+               instance_ = new RMShmCache();
+
        return instance_;
 }
 
 RMShmCache::RMShmCache(): ready_(false)
 {
-       Init();
+       if (!Init())
+               RM_ERR("Init is failed");
 }
 
 bool RMShmCache::Init(void)
 {
+       std::lock_guard<std::mutex> guard(init_mutex_);
+
        if (ready_)
                return true;
 
-       std::lock_guard<std::mutex> guard(init_mutex_);
-
        try {
                shm_segment_ = new shared::segment(boost::interprocess::open_read_only, "shm_rm");
                players_ = shm_segment_->find<shared::unordered_map<int, shared::string>>("players").first;
@@ -60,6 +60,7 @@ bool RMShmCache::Init(void)
        }
 
        ready_ = true;
+
        return true;
 }
 
@@ -173,6 +174,11 @@ int RMShmCache::FindPlayersUsingAudioMainOut(rm_resource_list_h *list)
        };
 
        rm_resource_list *result = (rm_resource_list*) calloc(1, sizeof(rm_resource_list));
+       if (!result) {
+               RM_ERR("rm_resource_list calloc failed");
+               return RM_ERROR;
+       }
+
        result->n_rsc = 0;
 
        for (unsigned int i = 0; i < ARRAY_SIZE(audio_main_out_devices); i++) {
@@ -183,6 +189,9 @@ int RMShmCache::FindPlayersUsingAudioMainOut(rm_resource_list_h *list)
                        continue;
 
                rm_resource *resource = (rm_resource*) calloc(1, sizeof(rm_resource));
+               if (!resource)
+                       continue;
+
                resource->id = audio_main_out_devices[i];
                resource->state = RM_RSC_STATE_EXCLUSIVE;
                resource->consumer_id = it->second.GetPlayerID();
index b07730126a83496270faf770a0912179acd637ab..fbf0c6e3ab6a60653a037d93401a53ee95163a56 100644 (file)
@@ -171,7 +171,7 @@ int rm_allocate_resources(int handle, const rm_category_request_s *requests, rm_
        RM_INFO("Request for Allocation handle[%d] / pid[%d] / requested #[%d]",
                                handle, getpid(), requests->request_num);
 
-       for (idx=0; idx < requests->request_num; idx++)
+       for (idx = 0; idx < requests->request_num; idx++)
                RM_INFO("(%d) CatID[%d-%s] / CatOpt[%d]",
                idx + 1, requests->category_id[idx], rm_convert_category_enum_to_string(requests->category_id[idx]), requests->category_option[idx]);
 
index 8bee3c2579231a1f1724c01e72fac3a9e922012e..75a2cf50ceb8cb07190c4d79a8d3106a08ac6626 100644 (file)
@@ -27,8 +27,7 @@
 #include <pthread.h>
 #include <assert.h>
 #include <list>
-
-#include <glib.h>
+#include <gio/gio.h>
 
 #include <rm_debug.h>
 #include <rm_module_api.h>
@@ -87,7 +86,7 @@ static gpointer rm_run_loop(gpointer data)
        if (main_loop != NULL)
                g_main_loop_run(main_loop);
 
-       RM_INFO("RM client thread finished\n");
+       RM_INFO("RM client thread finished");
        return NULL;
 }
 
@@ -111,12 +110,14 @@ int _create_event_loop(void)
                // LCOV_EXCL_STOP
        }
 
-       return RM_OK;
+       RM_INFO("RM_OK");
 
+       return RM_OK;
 }
 
 int _create_cb_root_dir(void)
 {
+       rm_return_code_e ret = RM_OK;
        const char *cb_dir_path = "/run/rsc_mgr";
 
        mode_t pmask;
@@ -129,15 +130,21 @@ int _create_cb_root_dir(void)
        mode_t dir_mode = 0666 | 0111;
        pmask = umask(0);
 
-       if (mkdir(cb_dir_path, dir_mode) !=0) {
+       if (mkdir(cb_dir_path, dir_mode) != 0) {
                RM_ERR("failed to create cb directory (%d)", errno);
-               umask(pmask);
-               return RM_ERROR;
+               ret = RM_ERROR;
+               goto exit;
        }
-       chmod(cb_dir_path, dir_mode);
+
+       if (chmod(cb_dir_path, dir_mode) == -1) {
+               RM_ERR("failed to create cb directory (%d)", errno);
+               ret = RM_ERROR;
+       }
+
+exit:
        umask(pmask);
 
-       return RM_OK;
+       return ret;
 // LCOV_EXCL_STOP
 }
 
@@ -155,7 +162,7 @@ int _create_FIFO_s2c(int pid, int cid)
 
        pmask = umask(0);
 
-       if (mknod(cb_path_s2c, S_IFIFO|0666, 0)) {
+       if (mknod(cb_path_s2c, S_IFIFO | 0666, 0)) {
                RM_ERR("failed to create cb for S2C(%s)-(%d)", cb_path_s2c, errno);
                umask(pmask);
                return RM_ERROR;
@@ -386,7 +393,7 @@ int rm_init_cb(int cid, rm_resource_cb cb, void *data)
        snprintf(cb_path_s2c, 256, "/run/rsc_mgr/%d.%d.s2c.cb", pid, cid);
 
        if ((fd = open(cb_path_s2c, O_RDWR|O_NONBLOCK)) < 0) {
-               RM_ERR("open error (%s), errno(%d)\n", cb_path_s2c, errno);
+               RM_ERR("open error (%s), errno(%d)", cb_path_s2c, errno);
                pthread_mutex_unlock(&rm_cb_mutex);
                return RM_ERROR;
        }
index 81a6703008eb782e4dc317bb09ffc0b2a4de25e5..01729fa5c115c4b3f2fc973dbee48f804c0e5b53 100644 (file)
@@ -43,7 +43,6 @@ void TCCallbackListener::ScalerStateChangeCb(rm_resource_list_h scalers, void *d
 
 TCCallbackListener::TCCallbackListener()
 {
-       m_n_notification = 0;
 }
 
 TCCallbackListener::~TCCallbackListener()
index 37abd41d3a7f1ca938ef96c20d57150a49bb573b..bcbe424de88fafc48f225f29ea0210ea3510e3e9 100644 (file)
@@ -39,7 +39,7 @@ public:
 
 private:
        std::map<int, int> m_scalers;
-       int m_n_notification;
+       int m_n_notification {};
 };
 
 #endif //__TC_CALLBACK_LISTENER_H__
index e7c97885720379d8d3c74a7c108ef673c90dbdf1..46b953fb81167120d65cd2a07620982b76b3ee2d 100644 (file)
 
 TCPlayer::TCPlayer()
 {
-       m_handle = 0;
-       m_allocated_rsc_index = 0;
-       m_rsc_index = 0;
-       m_query_index = 0;
-       m_n_conflict = 0;
-       m_n_conflicted_rsc = 0;
 }
 
 TCPlayer::~TCPlayer()
index 1cc1dfa2b4734374dc8eccf0fd52d8ccd9ecb4ed..63d3327285b284934564906c08d9710dfec3ec8c 100644 (file)
@@ -71,12 +71,12 @@ private:
        std::map<int, TCResource *> m_queries;
 
        std::string m_app_id;
-       int m_handle;
-       int m_rsc_index;
-       int m_query_index;
-       int m_allocated_rsc_index;
-       int m_n_conflict;
-       int m_n_conflicted_rsc;
+       int m_handle {};
+       int m_rsc_index {};
+       int m_query_index {};
+       int m_allocated_rsc_index {};
+       int m_n_conflict {};
+       int m_n_conflicted_rsc {};
 };
 
 #endif //__TC_PLAYER_H__
index fc61df0e4709ed8fee3a47a7ba6799d0325709d9..7bf824a3e6e3f56bef7f45dc61f6203a2285bb13 100644 (file)
@@ -18,9 +18,6 @@
 
 TCResource::TCResource(int category_id, int category_option, int state)
 {
-       m_category_id = category_id;
-       m_category_option = category_option;
-       m_state = state;
 }
 
 TCResource::~TCResource()
index c3068b4809eb67ae294aef1403e3184fbeacdbfb..23f1953d8043ad11445800ecd112fb13bc58d4b1 100644 (file)
@@ -20,7 +20,8 @@
 class TCResource
 {
 public:
-       TCResource(int category_id, int category_option, int state);
+       TCResource(int category_id, int category_option, int state)
+               : m_category_id(category_id), m_category_option(category_option), m_state(state) {}
        ~TCResource();
 
        int GetCategoryId(void) {return m_category_id;}
@@ -28,9 +29,9 @@ public:
        int GetState(void) {return m_state;}
 
 private:
-       int m_category_id;
-       int m_category_option;
-       int m_state;
+       int m_category_id {};
+       int m_category_option {};
+       int m_state {};
 };
 
 #endif //__TC_RESOURCE_H__
index d8b7ad2a12850b03686fbfaefed6b085a33adede..8a5d5d53e7fa6af77a553b70c8a4d8d92085b8c8 100644 (file)
@@ -41,7 +41,7 @@ GMainContext *get_callback_thread_context(void)
 
 GTEST_API_ int main(int argc, char **argv)
 {
-       RM_TEST_MSG("Running main() from ut_main.cpp\n");
+       RM_TEST_MSG("Running main() from ut_main.cpp");
 
        //GMainContext *context = g_main_context_new();
        g_main_loop = g_main_loop_new(NULL, FALSE);
index b894c25599e6888b44e51c4ae60ca1768c58de85..6152df22acf872b9d6d64616a5e13fabbe69aa09 100644 (file)
@@ -105,9 +105,9 @@ rm_cb_result conflict_cb4(int handle, rm_callback_type event, rm_device_request_
        return RM_CB_RESULT_OK;
 }
 
-int flag1=0;
-int flag2=0;
-int endflag=0;
+int flag1 = 0;
+int flag2 = 0;
+int endflag = 0;
 pthread_t t1,t2,t3;
 rm_cb_result cb1(int handle, rm_callback_type event, rm_device_request_s *info, void *data)
 {
@@ -124,7 +124,7 @@ rm_cb_result cb2(int handle, rm_callback_type event, rm_device_request_s *info,
 
 void *thread_main1(void*)
 {
-       int handle=0;
+       int handle = 0;
        EXPECT_EQ(RM_OK,rm_register(cb1, NULL, &handle, NULL));
 
        //alloc
@@ -151,7 +151,7 @@ void *thread_main1(void*)
 }
 void *thread_main2(void*)
 {
-       int handle=0;
+       int handle = 0;
        while (true) {
                if (flag1) {
                        EXPECT_EQ(RM_OK,rm_register(cb2, NULL, &handle, NULL));
@@ -167,7 +167,7 @@ void *thread_main2(void*)
                        rm_device_return_s *allocated = (rm_device_return_s*) malloc (sizeof(rm_device_return_s));
                        memset((void*)allocated, 0, sizeof(rm_device_return_s));
                        EXPECT_EQ(RM_OK,rm_allocate_resources(handle, request, allocated));
-                       flag1=0;
+                       flag1 = 0;
 
                }
                if (endflag) {
@@ -180,7 +180,7 @@ void *thread_main2(void*)
 
 void *thread_main3(void*)
 {
-       int handle=0;
+       int handle = 0;
        while (true) {
                if (flag2) {
                        EXPECT_EQ(RM_OK,rm_register(cb2, NULL, &handle, NULL));
@@ -1015,7 +1015,7 @@ TEST(ut_rm_api, rm_conflict_internal_msg_q)
        pthread_create(&t1,NULL,&thread_main1,(void*)1);
        pthread_create(&t2,NULL,&thread_main2,(void*)2);
        pthread_create(&t3,NULL,&thread_main3,(void*)3);
-       int status=0;
+       int status = 0;
        pthread_join(t1,(void**)&status);
        pthread_join(t2,(void**)&status);
        pthread_join(t3,(void**)&status);