Fix coverity/Svace issues (BAD_CHECK_OF_WAIT_COND, MISSING_UNLOCK, NULL_AFTER_DEREF) 55/304355/7 accepted/tizen_unified accepted/tizen_unified_toolchain accepted/tizen_unified_x tizen accepted/tizen/unified/20240131.175405 accepted/tizen/unified/toolchain/20240311.065435 accepted/tizen/unified/x/20240205.063846
authorGilbok Lee <gilbok.lee@samsung.com>
Tue, 16 Jan 2024 06:37:13 +0000 (15:37 +0900)
committerGilbok Lee <gilbok.lee@samsung.com>
Mon, 22 Jan 2024 02:52:52 +0000 (11:52 +0900)
- Remove unused volume mutex
- Add check wait condition
- Remove unnecessary code

[Version] 0.2.51
[Issue Type] Coverity

Change-Id: I9e4cd3ae97183c297cf5a963597f14cfeb6a4adc

packaging/libmm-radio.spec
src/include/mm_radio_priv_hal.h
src/include/mm_radio_utils.h
src/mm_radio_priv_emulator.c
src/mm_radio_priv_hal.c

index 980b195..90d68b3 100644 (file)
@@ -1,6 +1,6 @@
 Name:       libmm-radio
 Summary:    Multimedia Framework Radio Library
-Version:    0.2.50
+Version:    0.2.51
 Release:    0
 Group:      System/Libraries
 License:    Apache-2.0
index 6221545..b8185c4 100644 (file)
@@ -198,9 +198,6 @@ typedef struct {
 
        int freq;
 
-       /* command lock */
-       pthread_mutex_t volume_lock;
-
        float local_volume;
 
        /* region settings */
index c72da4a..759ef71 100644 (file)
@@ -125,9 +125,6 @@ do {                                                                                                                \
 #define MMRADIO_CMD_LOCK(x_radio)              pthread_mutex_lock(&((mm_radio_t *)x_radio)->cmd_lock)
 #define MMRADIO_CMD_UNLOCK(x_radio)            pthread_mutex_unlock(&((mm_radio_t *)x_radio)->cmd_lock)
 
-#define MMRADIO_VOLUME_LOCK(x_radio)           pthread_mutex_lock(&((mm_radio_t *)x_radio)->volume_lock)
-#define MMRADIO_VOLUME_UNLOCK(x_radio)         pthread_mutex_unlock(&((mm_radio_t *)x_radio)->volume_lock)
-
 /* msg/seek/scan thread */
 #define MMRADIO_THREAD_LOCK(x_thread_t)                pthread_mutex_lock(&x_thread_t->mutex)
 #define MMRADIO_THREAD_UNLOCK(x_thread_t)      pthread_mutex_unlock(&x_thread_t->mutex)
index d7f6cd1..8d899ba 100644 (file)
@@ -1557,14 +1557,10 @@ static void __mmradio_destroy_thread_type(mm_radio_t *radio, MMRadioThreadTypes
                switch (type) {
                case MM_RADIO_THREAD_MSG:
                        msg = g_slice_new0(mm_radio_msg_t);
-                       if (!msg) {
-                               MMRADIO_LOG_ERROR("failed to get mm_radio_msg_t");
-                       } else {
-                               msg->msg_type = MM_RADIO_MSG_DESTROY;
-                               g_async_queue_push_front(radio->msg_queue, msg);
-                               pthread_join(p_thread->thread, NULL);
-                               p_thread->thread = 0;
-                       }
+                       msg->msg_type = MM_RADIO_MSG_DESTROY;
+                       g_async_queue_push_front(radio->msg_queue, msg);
+                       pthread_join(p_thread->thread, NULL);
+                       p_thread->thread = 0;
                        break;
                case MM_RADIO_THREAD_SEEK:
                case MM_RADIO_THREAD_SCAN:
@@ -1608,10 +1604,6 @@ static void __mmradio_destroy_threads(mm_radio_t *radio)
 void __mmradio_msg_push(mm_radio_t *radio, MMRadioMsgTypes msg_type, int msg_data)
 {
        mm_radio_msg_t *msg = g_slice_new0(mm_radio_msg_t);
-       if (!msg) {
-               MMRADIO_LOG_ERROR("NULL msg pointer");
-               return;
-       }
 
        msg->msg_type = msg_type;
        msg->data = msg_data;
index c65f50e..c83627f 100644 (file)
@@ -719,10 +719,13 @@ int _mmradio_seek(mm_radio_t *radio, MMRadioSeekDirectionType direction)
        p_thread = &radio->thread[MM_RADIO_THREAD_SEEK];
        MMRADIO_CHECK_ARG(p_thread);
 
+       MMRADIO_THREAD_LOCK(p_thread);
        if (p_thread->is_running) {
                MMRADIO_LOG_ERROR("[RADIO_ERROR_INVALID_OPERATION]radio is seeking, can't serve another request try again");
+               MMRADIO_THREAD_UNLOCK(p_thread);
                return MM_ERROR_RADIO_INTERNAL;
        }
+       MMRADIO_THREAD_UNLOCK(p_thread);
 
        radio->seek_unmute = false;
 
@@ -776,6 +779,14 @@ int _mmradio_start_scan(mm_radio_t *radio)
        p_thread = &radio->thread[MM_RADIO_THREAD_SCAN];
        MMRADIO_CHECK_ARG(p_thread);
 
+       MMRADIO_THREAD_LOCK(p_thread);
+       if (p_thread->is_running) {
+               MMRADIO_LOG_ERROR("[MM_ERROR_RADIO_INVALID_STATE]radio is scanning, can't serve another request try again");
+               MMRADIO_THREAD_UNLOCK(p_thread);
+               return MM_ERROR_RADIO_INVALID_STATE;
+       }
+       MMRADIO_THREAD_UNLOCK(p_thread);
+
        p_thread->stop = false;
 
        ret = __mmradio_prepare_radio_device(radio);
@@ -851,14 +862,12 @@ void __mmradio_scan_thread(mm_radio_t *radio)
        MMRADIO_CHECK_ARG_RETURN_VOID(p_thread);
 
        MMRADIO_THREAD_LOCK(p_thread);
-       MMRADIO_THREAD_SIGNAL(p_thread);
-       MMRADIO_THREAD_UNLOCK(p_thread);
-
-       MMRADIO_THREAD_LOCK(p_thread);
 
        while (!p_thread->thread_exit) {
-               MMRADIO_LOG_DEBUG("scan thread started. waiting for signal.");
-               MMRADIO_THREAD_WAIT(p_thread);
+               while (!p_thread->is_running) {
+                       MMRADIO_LOG_DEBUG("scan thread started. waiting for signal.");
+                       MMRADIO_THREAD_WAIT(p_thread);
+               }
 
                if (p_thread->thread_exit) {
                        MMRADIO_LOG_DEBUG("exiting scan thread");
@@ -1022,14 +1031,12 @@ void __mmradio_seek_thread(mm_radio_t *radio)
        MMRADIO_CHECK_ARG_RETURN_VOID(p_thread);
 
        MMRADIO_THREAD_LOCK(p_thread);
-       MMRADIO_THREAD_SIGNAL(p_thread);
-       MMRADIO_THREAD_UNLOCK(p_thread);
-
-       MMRADIO_THREAD_LOCK(p_thread);
 
        while (!p_thread->thread_exit) {
-               MMRADIO_LOG_DEBUG("seek thread started. waiting for signal.");
-               MMRADIO_THREAD_WAIT(p_thread);
+               while (!p_thread->is_running) {
+                       MMRADIO_LOG_DEBUG("seek thread started. waiting for signal.");
+                       MMRADIO_THREAD_WAIT(p_thread);
+               }
 
                if (p_thread->thread_exit) {
                        MMRADIO_LOG_DEBUG("exiting seek thread");
@@ -1387,15 +1394,12 @@ int _mmradio_set_volume(mm_radio_t *radio, float volume)
 
        MMRADIO_LOG_INFO("Setting %f volume", volume);
 
-       MMRADIO_VOLUME_LOCK(radio);
        radio->local_volume = volume;
 
        if (radio->vstream)
                sound_manager_set_virtual_stream_volume(radio->vstream,
                        (double)radio->local_volume);
 
-       MMRADIO_VOLUME_UNLOCK(radio);
-
        MMRADIO_LOG_FLEAVE();
 
        return MM_ERROR_NONE;
@@ -1412,9 +1416,7 @@ int _mmradio_get_volume(mm_radio_t *radio, float *pVolume)
 
        MMRADIO_RETURN_VAL_IF_FAIL(pVolume, MM_ERROR_INVALID_ARGUMENT);
 
-       MMRADIO_VOLUME_LOCK(radio);
        *pVolume = radio->local_volume;
-       MMRADIO_VOLUME_UNLOCK(radio);
 
        MMRADIO_LOG_FLEAVE();
 
@@ -1464,25 +1466,19 @@ static int __mmradio_create_thread_type(mm_radio_t *radio, MMRadioThreadTypes ty
        }
 
        p_thread = &radio->thread[type];
+       p_thread->is_running = false;
        MMRADIO_CHECK_ARG(p_thread);
 
        MMRADIO_INIT_MUTEX(p_thread->mutex);
        MMRADIO_INIT_COND(p_thread->cond);
 
-       MMRADIO_THREAD_LOCK(p_thread);
        p_thread->thread_id = pthread_create(&p_thread->thread, NULL,
                (void *)__mmradio_thread_function[type], (void *)radio);
        if (p_thread->thread_id) {
                MMRADIO_LOG_DEBUG("failed to create thread : [%d]", type);
-               MMRADIO_THREAD_UNLOCK(p_thread);
                return MM_ERROR_RADIO_INTERNAL;
        }
 
-       MMRADIO_LOG_DEBUG("wait for [%d] thread", type);
-       MMRADIO_THREAD_WAIT(p_thread);
-       MMRADIO_LOG_DEBUG("[%d] thread started", type);
-       MMRADIO_THREAD_UNLOCK(p_thread);
-
        return MM_ERROR_NONE;
 
 ERROR:
@@ -1502,7 +1498,6 @@ static int __mmradio_create_threads(mm_radio_t *radio)
        MMRADIO_CHECK_INSTANCE(radio);
 
        MMRADIO_INIT_MUTEX(radio->cmd_lock);
-       MMRADIO_INIT_MUTEX(radio->volume_lock);
        MMRADIO_INIT_MUTEX(radio->hal_seek_mutex);
 
        for (type = (int)MM_RADIO_THREAD_MSG; type < (int)MM_RADIO_THREAD_NUM; type++) {
@@ -1520,7 +1515,6 @@ static int __mmradio_create_threads(mm_radio_t *radio)
 
 ERROR:
        pthread_mutex_destroy(&radio->cmd_lock);
-       pthread_mutex_destroy(&radio->volume_lock);
        pthread_mutex_destroy(&radio->hal_seek_mutex);
 
        MMRADIO_LOG_FLEAVE();
@@ -1560,6 +1554,7 @@ static void __mmradio_destroy_thread_type(mm_radio_t *radio, MMRadioThreadTypes
                case MM_RADIO_THREAD_SCAN:
                        MMRADIO_THREAD_LOCK(p_thread);
                        p_thread->thread_exit = true;
+                       p_thread->is_running = true;
                        MMRADIO_THREAD_SIGNAL(p_thread);
                        MMRADIO_THREAD_UNLOCK(p_thread);
                        pthread_join(p_thread->thread, NULL);
@@ -1589,7 +1584,6 @@ static void __mmradio_destroy_threads(mm_radio_t *radio)
                __mmradio_destroy_thread_type(radio, (MMRadioThreadTypes)type);
 
        pthread_mutex_destroy(&radio->cmd_lock);
-       pthread_mutex_destroy(&radio->volume_lock);
        pthread_mutex_destroy(&radio->hal_seek_mutex);
 
        MMRADIO_LOG_FLEAVE();
@@ -1625,11 +1619,6 @@ void __mmradio_msg_thread(mm_radio_t *radio)
 
        p_thread->thread_exit = false;
 
-
-       MMRADIO_THREAD_LOCK(p_thread);
-       MMRADIO_THREAD_SIGNAL(p_thread);
-       MMRADIO_THREAD_UNLOCK(p_thread);
-
        /* we run a while one loop*/
        while (!p_thread->thread_exit) {
                msg = (mm_radio_msg_t *)g_async_queue_pop(radio->msg_queue);
@@ -1719,8 +1708,6 @@ static int __mmradio_prepare_radio_device(mm_radio_t *radio)
        ret = mm_resource_manager_commit(radio->resource_manager);
        if (ret != MM_RESOURCE_MANAGER_ERROR_NONE) {
                MMRADIO_LOG_ERROR("failed to commit resource manager");
-               mm_resource_manager_mark_for_release(radio->resource_manager,
-                       radio->radio_resource);
                radio->radio_resource = NULL;
                return ret;
        }