From c2480915284707383ba8be1650c41f95340a1633 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20P=C5=91cze?= Date: Sun, 5 Jun 2022 15:43:43 +0200 Subject: [PATCH] pulse-server: module-switch-on-connect: remove dead code and one allocation As Coverity correctly points out, the `if (blocklist)` condition is never true after the `out` label, so this commit makes some changes to remove the dead code. First of all, the `regex_t` object is directly embedded in the module's data struct, so the `malloc()` call can be removed, and thus there is no need for the cleanup code anymore, so everything after the `out` label is also removed. Furthermore, two NULL checks are removed which check `d->blocklist` from `module_switch_on_connect_unload()` and `manager_added()` because both of those functions can only ever run if the `d->blocklist` regex object has been successfully initialized in `module_switch_on_connect_prepare()`. Those checks were not strictly needed to begin with. --- .../modules/module-switch-on-connect.c | 34 +++---------------- 1 file changed, 5 insertions(+), 29 deletions(-) diff --git a/src/modules/module-protocol-pulse/modules/module-switch-on-connect.c b/src/modules/module-protocol-pulse/modules/module-switch-on-connect.c index f4b0bb230..7e30cd54d 100644 --- a/src/modules/module-protocol-pulse/modules/module-switch-on-connect.c +++ b/src/modules/module-protocol-pulse/modules/module-switch-on-connect.c @@ -52,7 +52,7 @@ struct module_switch_on_connect_data { struct spa_hook manager_listener; struct pw_manager_object *metadata_default; - regex_t *blocklist; + regex_t blocklist; int sync_seq; @@ -122,7 +122,7 @@ static void manager_added(void *data, struct pw_manager_object *o) return; } - if (d->blocklist && regexec(d->blocklist, name, 0, NULL, 0) == 0) { + if (regexec(&d->blocklist, name, 0, NULL, 0) == 0) { pw_log_debug("not switching to blocklisted device"); return; } @@ -224,11 +224,7 @@ static int module_switch_on_connect_unload(struct module *module) d->core = NULL; } - if (d->blocklist) { - regfree(d->blocklist); - free(d->blocklist); - d->blocklist = NULL; - } + regfree(&d->blocklist); return 0; } @@ -249,10 +245,8 @@ static int module_switch_on_connect_prepare(struct module * const module) { struct module_switch_on_connect_data * const d = module->user_data; struct pw_properties * const props = module->props; - regex_t *blocklist = NULL; bool only_from_unavailable = false, ignore_virtual = true; const char *str; - int res; PW_LOG_TOPIC_INIT(mod_topic); @@ -266,25 +260,15 @@ static int module_switch_on_connect_prepare(struct module * const module) pw_properties_set(props, "ignore_virtual", NULL); } - if ((blocklist = malloc(sizeof(regex_t))) == NULL) { - res = -ENOMEM; - goto out; - } - if ((str = pw_properties_get(props, "blocklist")) == NULL) str = DEFAULT_BLOCKLIST; - if ((res = regcomp(blocklist, str, REG_NOSUB | REG_EXTENDED)) != 0) { - free(blocklist); - blocklist = NULL; - res = -EINVAL; - goto out; - } + if (regcomp(&d->blocklist, str, REG_NOSUB | REG_EXTENDED) != 0) + return -EINVAL; pw_properties_set(props, "blocklist", NULL); d->module = module; - d->blocklist = blocklist; d->ignore_virtual = ignore_virtual; d->only_from_unavailable = only_from_unavailable; @@ -294,14 +278,6 @@ static int module_switch_on_connect_prepare(struct module * const module) } return 0; - -out: - if (blocklist) { - regfree(blocklist); - free(blocklist); - } - - return res; } DEFINE_MODULE_INFO(module_switch_on_connect) = {