module_mpris: Fixed memory leak

The identity list now uses tlllist and is deallocated properly
This commit is contained in:
haruInDisguise 2025-03-10 01:36:26 +01:00
parent dcf936fd9b
commit 0bcde5c453

View file

@ -83,11 +83,6 @@ struct context {
sd_bus *monitor_connection; sd_bus *monitor_connection;
sd_bus_message *update_message; sd_bus_message *update_message;
/* FIXME: There is no nice way to pass the desired identities to
* the event handler for validation. */
char **identities_ref;
size_t identities_count;
tll(struct client *) clients; tll(struct client *) clients;
struct client *current_client; struct client *current_client;
@ -99,14 +94,14 @@ struct private
thrd_t refresh_thread_id; thrd_t refresh_thread_id;
int refresh_abort_fd; int refresh_abort_fd;
size_t identities_count;
size_t timeout_ms; size_t timeout_ms;
const char **identities;
struct particle *label; struct particle *label;
struct context context; struct context context;
}; };
static tll(const char*) identity_list;
#if 0 #if 0
static void static void
debug_print_argument_type(sd_bus_message *message) debug_print_argument_type(sd_bus_message *message)
@ -225,10 +220,10 @@ client_change_unique_name(struct client *client, const char *new_name)
} }
static bool static bool
verify_bus_name(char **idents, const size_t ident_count, const char *name) verify_bus_name(const char *name)
{ {
for (size_t i = 0; i < ident_count; i++) { tll_foreach(identity_list, it) {
const char *ident = idents[i]; const char *ident = it->item;
if (strlen(name) < strlen(BUS_NAME ".") + strlen(ident)) { if (strlen(name) < strlen(BUS_NAME ".") + strlen(ident)) {
continue; continue;
@ -455,9 +450,13 @@ destroy(struct module *mod)
sd_bus_close(context->monitor_connection); sd_bus_close(context->monitor_connection);
module_default_destroy(mod); tll_foreach(identity_list, it) {
free((char*)it->item);
}
m->label->destroy(m->label); m->label->destroy(m->label);
free(m); free(m);
module_default_destroy(mod);
} }
static void static void
@ -524,7 +523,7 @@ context_event_handle_name_acquired(sd_bus_message *message, struct context *cont
return; return;
} }
if (verify_bus_name(context->identities_ref, context->identities_count, name)) { if (verify_bus_name(name)) {
const char *unique_name = sd_bus_message_get_destination(message); const char *unique_name = sd_bus_message_get_destination(message);
LOG_DBG("'NameAcquired': Acquired new client: %s unique: %s", name, unique_name); LOG_DBG("'NameAcquired': Acquired new client: %s unique: %s", name, unique_name);
client_add(context, name, unique_name); client_add(context, name, unique_name);
@ -667,8 +666,6 @@ context_new(struct private *m, struct context *context)
(*context) = (struct context){ (*context) = (struct context){
.monitor_connection = connection, .monitor_connection = connection,
.identities_ref = (char **)m->identities,
.identities_count = m->identities_count,
.clients = tll_init(), .clients = tll_init(),
}; };
@ -1061,8 +1058,8 @@ run(struct module *mod)
bar->refresh(bar); bar->refresh(bar);
} }
LOG_DBG("exiting");
LOG_DBG("exiting");
return ret; return ret;
} }
@ -1073,17 +1070,11 @@ description(const struct module *mod)
} }
static struct module * static struct module *
mpris_new(const char **ident, size_t ident_count, size_t timeout, struct particle *label) mpris_new(size_t timeout, struct particle *label)
{ {
struct private *priv = calloc(1, sizeof(*priv)); struct private *priv = calloc(1, sizeof(*priv));
priv->label = label; priv->label = label;
priv->timeout_ms = timeout; priv->timeout_ms = timeout;
priv->identities = malloc(sizeof(*ident) * ident_count);
priv->identities_count = ident_count;
for (size_t i = 0; i < ident_count; i++) {
priv->identities[i] = strdup(ident[i]);
}
struct module *mod = module_common_new(); struct module *mod = module_common_new();
mod->private = priv; mod->private = priv;
@ -1106,14 +1097,14 @@ from_conf(const struct yml_node *node, struct conf_inherit inherited)
if(query_timeout != NULL) if(query_timeout != NULL)
timeout_ms = yml_value_as_int(query_timeout) * 1000; timeout_ms = yml_value_as_int(query_timeout) * 1000;
const size_t ident_count = yml_list_length(ident_list); // FIXME: This is a redundant copy
const char *ident[ident_count];
size_t i = 0; size_t i = 0;
for (struct yml_list_iter iter = yml_list_iter(ident_list); iter.node != NULL; yml_list_next(&iter), i++) { for (struct yml_list_iter iter = yml_list_iter(ident_list); iter.node != NULL; yml_list_next(&iter), i++) {
ident[i] = yml_value_as_string(iter.node); const char *string = strdup(yml_value_as_string(iter.node));
tll_push_back(identity_list, string);
} }
return mpris_new(ident, ident_count, timeout_ms, conf_to_particle(c, inherited)); return mpris_new(timeout_ms, conf_to_particle(c, inherited));
} }
static bool static bool