device-manager: Fix svace issues (DEREF_OF_NULL.RET.STAT) 23/271523/3 accepted/tizen/unified/20220223.132451 submit/tizen/20220222.235015
authorSeungbae Shin <seungbae.shin@samsung.com>
Tue, 22 Feb 2022 10:34:43 +0000 (19:34 +0900)
committerSeungbae Shin <seungbae.shin@samsung.com>
Tue, 22 Feb 2022 11:31:41 +0000 (20:31 +0900)
- Add null check for pulse_device_get_proplist()
- Use foreach macro for dynarray on handle_internal_pulse_device()
- Remove unnecessary braces
- Fix some invalid indents
- Revise some code format

[Version] 15.0.1
[Issue Type] Svace

Change-Id: I9ae16c35194f94633063b38b49ff133d9372538d

packaging/pulseaudio-modules-tizen.spec
src/device-manager.c

index 2194382929670dbb95b86b12d412935c63458ab0..f19401efae6a871bdf1c6cc5f10fbe5a1ec92348 100644 (file)
@@ -2,7 +2,7 @@
 
 Name:             pulseaudio-modules-tizen
 Summary:          Pulseaudio modules for Tizen
-Version:          15.0.0
+Version:          15.0.1
 Release:          0
 Group:            Multimedia/Audio
 License:          LGPL-2.1+
index 7cb46c961896e1d3a86a959b95bc1bd50e3d32b9..510ef2469ac2df4a0d8cadb228a73a5fb1ce06f6 100644 (file)
@@ -315,21 +315,19 @@ static void file_info_free_func(struct device_file_info *file_info) {
 }
 
 static dm_device_class_t device_string_get_class(const char *device_string) {
-    if (!device_string) {
+    if (!device_string)
         return DM_DEVICE_CLASS_NONE;
-    }
 
-    if (device_string == strstr(device_string, "alsa")) {
+    if (device_string == strstr(device_string, "alsa"))
         return DM_DEVICE_CLASS_ALSA;
-    } else if (device_string == strstr(device_string, "null")) {
+    else if (device_string == strstr(device_string, "null"))
         return DM_DEVICE_CLASS_NULL;
-    } else if (device_string == strstr(device_string, "tizen")) {
+    else if (device_string == strstr(device_string, "tizen"))
         return DM_DEVICE_CLASS_TIZEN;
-    } else if (device_string == strstr(device_string, "acm")) {
+    else if (device_string == strstr(device_string, "acm"))
         return DM_DEVICE_CLASS_ACM;
-    } else {
-        return DM_DEVICE_CLASS_NONE;
-    }
+
+    return DM_DEVICE_CLASS_NONE;
 }
 
 /* device_string looks like "alsa:0,0" or "tizen:0,0"
@@ -359,46 +357,37 @@ static int device_string_get_name(const char *device_string, char *name) {
 }
 
 static pa_proplist* pulse_device_get_proplist(pa_object *pdevice) {
-    if (pa_sink_isinstance(pdevice))
-        return PA_SINK(pdevice)->proplist;
-    else
-        return PA_SOURCE(pdevice)->proplist;
+    if (!pdevice)
+        return NULL;
+
+    return pa_sink_isinstance(pdevice) ? PA_SINK(pdevice)->proplist :
+                                         PA_SOURCE(pdevice)->proplist;
 }
 
 static bool pulse_device_is_alsa(pa_object *pdevice) {
     const char *api_name = NULL;
-    pa_proplist *prop;
+    pa_proplist *prop = pulse_device_get_proplist(pdevice);
 
-    if ((prop = pulse_device_get_proplist(pdevice)) == NULL)
+    if (!prop)
         return false;
 
-    if ((api_name = pa_proplist_gets(prop, PA_PROP_DEVICE_API))) {
-        if (pa_safe_streq(api_name, DEVICE_API_ALSA)) {
-            return true;
-        } else {
-            return false;
-        }
-    } else {
-        return false;
-    }
+    if ((api_name = pa_proplist_gets(prop, PA_PROP_DEVICE_API)))
+        return pa_safe_streq(api_name, DEVICE_API_ALSA);
+
+    return false;
 }
 
 static bool pulse_device_is_bluez(pa_object *pdevice) {
     const char *api_name = NULL;
-    pa_proplist *prop;
+    pa_proplist *prop = pulse_device_get_proplist(pdevice);
 
-    if ((prop = pulse_device_get_proplist(pdevice)) == NULL)
+    if (!prop)
         return false;
 
-    if ((api_name = pa_proplist_gets(prop, PA_PROP_DEVICE_API))) {
-        if (pa_safe_streq(api_name, DEVICE_API_BLUEZ)) {
-            return true;
-        } else {
-            return false;
-        }
-    } else {
-        return false;
-    }
+    if ((api_name = pa_proplist_gets(prop, PA_PROP_DEVICE_API)))
+        return pa_safe_streq(api_name, DEVICE_API_BLUEZ);
+
+    return false;
 }
 
 static bool pulse_device_is_acm(pa_object *pdevice) {
@@ -445,37 +434,26 @@ static bool pulse_device_is_tizenaudio(pa_object *pdevice) {
 
 static bool pulse_device_is_usb(pa_object *pdevice) {
     const char *bus_name = NULL;
-    pa_proplist *prop;
+    pa_proplist *prop = pulse_device_get_proplist(pdevice);
 
-    if ((prop = pulse_device_get_proplist(pdevice)) == NULL)
+    if (!prop)
         return false;
 
-    if ((bus_name = pa_proplist_gets(prop, PA_PROP_DEVICE_BUS))) {
-        if (pa_safe_streq(bus_name, DEVICE_BUS_USB)) {
-            return true;
-        } else {
-            return false;
-        }
-    } else {
-        pa_log_debug("This device doesn't have property '%s'", PA_PROP_DEVICE_BUS);
-        return false;
-    }
+    if ((bus_name = pa_proplist_gets(prop, PA_PROP_DEVICE_BUS)))
+        return pa_safe_streq(bus_name, DEVICE_BUS_USB);
+
+    pa_log_debug("This device doesn't have property '%s'", PA_PROP_DEVICE_BUS);
+    return false;
 }
 
 static bool pulse_device_is_null(pa_object *pdevice) {
-    pa_sink *sink;
-    pa_source *source;
-
     if (!pdevice)
         return false;
 
-    if (pa_sink_isinstance(pdevice)) {
-        sink = PA_SINK(pdevice);
-        return pa_safe_streq(sink->module->name, "module-null-sink");
-    } else {
-        source = PA_SOURCE(pdevice);
-        return pa_safe_streq(source->module->name, "module-null-source");
-    }
+    if (pa_sink_isinstance(pdevice))
+        return pa_safe_streq(PA_SINK(pdevice)->module->name, "module-null-sink");
+    else
+        return pa_safe_streq(PA_SOURCE(pdevice)->module->name, "module-null-source");
 }
 
 static bool pulse_device_is_rtsp(pa_object *pdevice) {
@@ -505,33 +483,24 @@ static const char* pulse_device_get_device_string_removed_argument(pa_object *pd
     const char *params_p, *params;
     char *end_p = NULL;
     int len = 0, prev_len = 0;
-    pa_sink *sink;
-    pa_source *source;
 
-    if (pa_sink_isinstance(pdevice)) {
-        sink = PA_SINK(pdevice);
-        params = sink->module->argument;
-    } else {
-        source = PA_SOURCE(pdevice);
-        params = source->module->argument;
-    }
+    params = pa_sink_isinstance(pdevice) ? PA_SINK(pdevice)->module->argument :
+                                           PA_SOURCE(pdevice)->module->argument;
 
     params_p = params;
 
-    if (!params) {
+    if (!params)
         return NULL;
-    }
-    if (!(device_string_p = strstr(params, "device="))) {
+
+    if (!(device_string_p = strstr(params, "device=")))
         return params;
-    }
 
     next_p = device_string_p;
-    while (!isblank(*next_p)) {
+    while (!isblank(*next_p))
         next_p++;
-    }
-    while (isblank(*next_p)) {
+
+    while (isblank(*next_p))
         next_p++;
-    }
 
     pa_strlcpy(removed_param, next_p, DEVICE_PARAM_STRING_MAX);
 
@@ -554,38 +523,33 @@ static dm_device_class_t pulse_device_get_class(pa_object *pdevice) {
         return DM_DEVICE_CLASS_NONE;
     }
 
-    if (pulse_device_is_null(pdevice)) {
+    if (pulse_device_is_null(pdevice))
         return DM_DEVICE_CLASS_NULL;
-    } else if (pulse_device_is_alsa(pdevice)) {
+    else if (pulse_device_is_alsa(pdevice))
         return DM_DEVICE_CLASS_ALSA;
-    } else if (pulse_device_is_tizenaudio(pdevice)) {
+    else if (pulse_device_is_tizenaudio(pdevice))
         return DM_DEVICE_CLASS_TIZEN;
-    } else if (pulse_device_is_bluez(pdevice)) {
+    else if (pulse_device_is_bluez(pdevice))
         return DM_DEVICE_CLASS_BT;
-    } else if (pulse_device_is_acm(pdevice)) {
+    else if (pulse_device_is_acm(pdevice))
         return DM_DEVICE_CLASS_ACM;
-    } else {
-        return DM_DEVICE_CLASS_NONE;
-    }
+
+    return DM_DEVICE_CLASS_NONE;
 }
 
 static dm_device_direction_t pulse_device_get_direction(pa_object *pdevice) {
-    if (pa_sink_isinstance(pdevice))
-        return DM_DEVICE_DIRECTION_OUT;
-    else
-        return DM_DEVICE_DIRECTION_IN;
+    return pa_sink_isinstance(pdevice) ? DM_DEVICE_DIRECTION_OUT : DM_DEVICE_DIRECTION_IN;
 }
 
 static bool pulse_device_is_monitor(pa_object *pdevice) {
     const char *device_class = NULL;
-    pa_proplist *prop;
+    pa_proplist *prop = pulse_device_get_proplist(pdevice);
 
-    prop = pulse_device_get_proplist(pdevice);
+    if (!prop)
+        return false;
 
-    if ((device_class = pa_proplist_gets(prop, PA_PROP_DEVICE_CLASS))) {
-        if (pa_safe_streq(device_class, DEVICE_CLASS_MONITOR))
-            return true;
-    }
+    if ((device_class = pa_proplist_gets(prop, PA_PROP_DEVICE_CLASS)))
+        return pa_safe_streq(device_class, DEVICE_CLASS_MONITOR);
 
     return false;
 }
@@ -671,9 +635,8 @@ static struct device_file_info* _device_manager_get_file_info(pa_idxset *file_in
     pa_assert(file_infos);
 
     PA_IDXSET_FOREACH(file_info, file_infos, file_idx) {
-        if (pa_safe_streq(file_info->device_string, device_string)) {
+        if (pa_safe_streq(file_info->device_string, device_string))
             return file_info;
-        }
     }
 
     return NULL;
@@ -694,7 +657,7 @@ static struct device_status_info* _device_status_new(const char *type,
 
 static void _device_status_free(struct device_status_info *status_info) {
     if (!status_info)
-        return ;
+        return;
 
     pa_xfree(status_info->type);
     pa_xfree(status_info->name);
@@ -773,7 +736,7 @@ void device_set_detected(pa_device_manager *manager, const char *type,
     pa_assert(type);
 
     if (!device_type_is_need_detect(type))
-        return ;
+        return;
 
     pa_log_info("Set device detected, type(%s) system_id(%s) -> %s(%d)",
             type, pa_strempty(system_id),
@@ -832,10 +795,10 @@ pa_tz_device* device_list_get_device_by_id(pa_device_manager *manager, uint32_t
     pa_assert(manager->device_list);
 
     PA_IDXSET_FOREACH(device, manager->device_list, idx) {
-        if (pa_tz_device_get_id(device) == id) {
+        if (pa_tz_device_get_id(device) == id)
             return device;
-        }
     }
+
     return NULL;
 }
 
@@ -923,18 +886,11 @@ finish:
 static bool pulse_device_params_is_equal(pa_object *pdevice, const char *params) {
     const char *removed_module_args;
     const char *module_args;
-    pa_sink *sink;
-    pa_source *source;
 
     pa_assert(pdevice);
 
-    if (pa_sink_isinstance(pdevice)) {
-        sink = PA_SINK(pdevice);
-        module_args = sink->module->argument;
-    } else {
-        source = PA_SOURCE(pdevice);
-        module_args = source->module->argument;
-    }
+    module_args = pa_sink_isinstance(pdevice) ? PA_SINK(pdevice)->module->argument :
+                                                PA_SOURCE(pdevice)->module->argument;
 
     if (!params && !module_args)
         return 0;
@@ -942,6 +898,7 @@ static bool pulse_device_params_is_equal(pa_object *pdevice, const char *params)
         return -1;
 
     removed_module_args = pulse_device_get_device_string_removed_argument(pdevice);
+
     return device_params_is_equal(params, removed_module_args);
 }
 
@@ -1189,13 +1146,10 @@ static int pulse_device_get_product_id(pa_object *pdevice) {
 static void pulse_device_set_use_internal_codec(pa_object *pdevice, bool use_internal_codec) {
     pa_assert(pdevice);
 
-    if (pa_sink_isinstance(pdevice)) {
-        pa_sink *sink = PA_SINK(pdevice);
-        sink->use_internal_codec = use_internal_codec;
-    } else {
-        pa_source *source = PA_SOURCE(pdevice);
-        source->use_internal_codec = use_internal_codec;
-    }
+    if (pa_sink_isinstance(pdevice))
+        PA_SINK(pdevice)->use_internal_codec = use_internal_codec;
+    else
+        PA_SOURCE(pdevice)->use_internal_codec = use_internal_codec;
 }
 
 /* Get system_id of physical device, it should be a unique id */
@@ -1203,6 +1157,7 @@ static const char* pulse_device_get_system_id(pa_object *pdevice) {
     pa_proplist *prop;
 
     prop = pulse_device_get_proplist(pdevice);
+
     if (pulse_device_is_usb(pdevice))
         return pa_proplist_gets(prop, "sysfs.path");
     else if (pulse_device_is_bluez(pdevice))
@@ -1217,8 +1172,8 @@ static const char* pulse_device_get_system_id(pa_object *pdevice) {
     else if (pulse_device_is_btsco(pdevice))
         return pa_proplist_gets(prop, PA_PROP_DEVICE_DESCRIPTION);
 #endif
-    else
-        return NULL;
+
+    return NULL;
 }
 
 static pa_sink* _core_get_sink(pa_core *core, const char *device_string, const char *params) {
@@ -1231,12 +1186,10 @@ static pa_sink* _core_get_sink(pa_core *core, const char *device_string, const c
     PA_IDXSET_FOREACH(sink, core->sinks, device_idx) {
         if (pulse_device_is_monitor(PA_OBJECT(sink)))
             continue;
-        if (pulse_device_same_device_string(PA_OBJECT(sink), device_string)) {
-            if (params == NULL)
-                return sink;
-            else if (pulse_device_params_is_equal(PA_OBJECT(sink), params))
+
+        if (pulse_device_same_device_string(PA_OBJECT(sink), device_string))
+            if (params == NULL || pulse_device_params_is_equal(PA_OBJECT(sink), params))
                 return sink;
-        }
     }
 
     return NULL;
@@ -1252,12 +1205,10 @@ static pa_source* _core_get_source(pa_core *core, const char *device_string, con
     PA_IDXSET_FOREACH(source, core->sources, device_idx) {
         if (pulse_device_is_monitor(PA_OBJECT(source)))
             continue;
-        if (pulse_device_same_device_string(PA_OBJECT(source), device_string)) {
-            if (params == NULL)
-                return source;
-            else if (pulse_device_params_is_equal(PA_OBJECT(source), params))
+
+        if (pulse_device_same_device_string(PA_OBJECT(source), device_string))
+            if (params == NULL || pulse_device_params_is_equal(PA_OBJECT(source), params))
                 return source;
-        }
     }
 
     return NULL;
@@ -1688,6 +1639,7 @@ static void handle_internal_pulse_device(pa_object *pdevice, bool is_loaded, pa_
     struct composite_type *ctype;
     pa_dynarray *ctypes;
     dm_device_direction_t direction;
+    int idx;
 
     pa_assert(pdevice);
     pa_assert(dm);
@@ -1698,38 +1650,27 @@ static void handle_internal_pulse_device(pa_object *pdevice, bool is_loaded, pa_
     /* Get types which this pulse_device belongs to */
     if ((ctypes = pulse_device_get_belongs_type(pdevice, dm)) == NULL) {
         pa_log_debug("Failed to get device type. Skip this");
-        return ;
+        return;
     }
 
-    if (is_loaded) {
-        /* Put this pulse_device to already loaded devices */
-        for (int i = 0; i < pa_dynarray_size(ctypes); i++) {
-            ctype = pa_dynarray_get(ctypes, i);
-            pa_log_info("Found belongs type %s.%s", ctype->type, ctype->role);
-            if ((device = device_list_get_device(dm, ctype->type, ctype->role, NULL))) {
-                pa_log_info("Add this pulse_device to device(%u)", pa_tz_device_get_id(device));
+    /* Put/Remove this pulse_device to already loaded devices */
+    PA_DYNARRAY_FOREACH(ctype, ctypes, idx) {
+        pa_log_info("Found belongs type %s.%s", ctype->type, ctype->role);
+        if ((device = device_list_get_device(dm, ctype->type, ctype->role, NULL))) {
+            pa_log_info("%s this pulse_device to device(%u)", is_loaded ? "Add" : "Remove", pa_tz_device_get_id(device));
+            if (is_loaded) {
                 if (direction == DM_DEVICE_DIRECTION_OUT)
                     pa_tz_device_add_sink(device, ctype->role, PA_SINK(pdevice));
                 else
                     pa_tz_device_add_source(device, ctype->role, PA_SOURCE(pdevice));
             } else {
-                pa_log_info("No device for %s.%s", ctype->type, ctype->role);
-            }
-        }
-    } else {
-        /* Remove this pulse_device from already loaded devices */
-        for (int i = 0; i < pa_dynarray_size(ctypes); i++) {
-            ctype = pa_dynarray_get(ctypes, i);
-            pa_log_info("Found belongs type %s.%s", ctype->type, ctype->role);
-            if ((device = device_list_get_device(dm, ctype->type, ctype->role, NULL))) {
-                pa_log_info("Remove this pulse_device from device(%u)", pa_tz_device_get_id(device));
                 if (direction == DM_DEVICE_DIRECTION_OUT)
                     pa_tz_device_remove_sink(device, PA_SINK(pdevice));
                 else
                     pa_tz_device_remove_source(device, PA_SOURCE(pdevice));
-            } else {
-                pa_log_info("No device for %s.%s", ctype->type, ctype->role);
             }
+        } else {
+            pa_log_info("No device for %s.%s", ctype->type, ctype->role);
         }
     }
 
@@ -1772,10 +1713,10 @@ static pa_hook_result_t sink_put_hook_callback(pa_core *c, pa_sink *sink, pa_dev
         handle_external_btsco_pulse_device(PA_OBJECT(sink), true, dm);
         return PA_HOOK_OK;
 #endif
-    } else {
-        pa_log_debug("Don't care this sink");
     }
 
+    pa_log_debug("Don't care this sink");
+
     return PA_HOOK_OK;
 }
 
@@ -1821,10 +1762,10 @@ static pa_hook_result_t sink_unlink_hook_callback(pa_core *c, pa_sink *sink, pa_
         handle_external_btsco_pulse_device(PA_OBJECT(sink), false, dm);
         return PA_HOOK_OK;
 #endif
-    } else {
-        pa_log_debug("Don't care this sink");
     }
 
+    pa_log_debug("Don't care this sink");
+
     return PA_HOOK_OK;
 }
 
@@ -1859,10 +1800,10 @@ static pa_hook_result_t source_put_hook_callback(pa_core *c, pa_source *source,
         pulse_device_set_use_internal_codec(PA_OBJECT(source), false);
         handle_external_btsco_pulse_device(PA_OBJECT(source), true, dm);
 #endif
-    } else {
-        pa_log_debug("Don't care this source");
     }
 
+    pa_log_debug("Don't care this source");
+
     return PA_HOOK_OK;
 }
 
@@ -1904,10 +1845,10 @@ static pa_hook_result_t source_unlink_hook_callback(pa_core *c, pa_source *sourc
         handle_external_btsco_pulse_device(PA_OBJECT(source), false, dm);
         return PA_HOOK_OK;
 #endif
-    } else {
-        pa_log_debug("Don't care this source");
     }
 
+    pa_log_debug("Don't care this source");
+
     return PA_HOOK_OK;
 }
 
@@ -1962,6 +1903,7 @@ static pa_hook_result_t sink_proplist_changed(pa_core *core, pa_sink *sink, pa_d
         pulse_device_set_use_internal_codec(PA_OBJECT(sink), false);
         handle_tunnel_pulse_device(PA_OBJECT(sink), pa_proplist_remote_is_allowed(sink->proplist), dm);
     }
+
     return PA_HOOK_OK;
 }
 
@@ -2245,11 +2187,9 @@ void handle_device_connected(pa_device_manager *dm, const char *type, const char
             pa_tz_device_new_data_done(&data);
         } else {
             pa_log_error("Invalid case : not static direction");
-            return ;
+            return;
         }
     }
-
-    return ;
 }
 
 static int load_builtin_devices(pa_device_manager *dm) {
@@ -2621,10 +2561,10 @@ static void device_type_status_init(pa_device_manager *manager) {
                 pa_log_warn("Unknown earjack status : %d", earjack_status);
 
         } else if (device_type_is_equal(type, DEVICE_TYPE_BT_SCO)) {
-                device_set_detected(manager, type, NULL, NULL, DEVICE_DISCONNECTED);
+            device_set_detected(manager, type, NULL, NULL, DEVICE_DISCONNECTED);
 
         } else if (device_type_is_equal(type, DEVICE_TYPE_HDMI)) {
-                device_set_detected(manager, type, NULL, NULL, DEVICE_DISCONNECTED);
+            device_set_detected(manager, type, NULL, NULL, DEVICE_DISCONNECTED);
 
         } else if (device_type_is_equal(type, DEVICE_TYPE_FORWARDING)) {
             int miracast_wfd_status = 0;
@@ -2639,7 +2579,6 @@ static void device_type_status_init(pa_device_manager *manager) {
             device_set_detected(manager, type, NULL, NULL, DEVICE_CONNECTED);
         }
     }
-    return ;
 }
 
 static pa_sink* load_sink(pa_device_manager *dm, const char *type, const char *role) {
@@ -2849,8 +2788,6 @@ void unload_acm_sink(pa_device_manager *dm) {
     }
 
     unload_sink(dm, DEVICE_TYPE_NETWORK, DEVICE_ROLE_ACM);
-
-    return;
 }
 
 pa_idxset* pa_device_manager_get_device_list(pa_device_manager *dm) {