From e309e221f7363addbee4462ddbb8c10b6abe7839 Mon Sep 17 00:00:00 2001 From: Hannes Winkler Date: Mon, 16 Sep 2024 21:17:33 +0000 Subject: [PATCH] try another source code permutation to fix analyzer warnings --- src/pluginregistry.c | 37 +++++++++++++++++++++---------------- src/util/list.h | 9 +++------ 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/pluginregistry.c b/src/pluginregistry.c index de51cb80..f78b917b 100644 --- a/src/pluginregistry.c +++ b/src/pluginregistry.c @@ -91,13 +91,11 @@ static struct plugin_instance *get_plugin_by_name(struct plugin_registry *regist // clang-format off static struct platch_obj_cb_data *get_cb_data_by_channel_locked(struct plugin_registry *registry, const char *channel) { - ANALYZER_SUPPRESS({ - list_for_each_entry(struct platch_obj_cb_data, data, ®istry->callbacks, entry) { - if (streq(data->channel, channel)) { - return data; - } + list_for_each_entry(struct platch_obj_cb_data, data, ®istry->callbacks, entry) { + if (streq(data->channel, channel)) { + return data; } - }); + } return NULL; } @@ -396,17 +394,24 @@ int plugin_registry_set_receiver(const char *channel, enum platch_codec codec, p int plugin_registry_remove_receiver_v2_locked(struct plugin_registry *registry, const char *channel) { struct platch_obj_cb_data *data; - // clang analyzer reports false-positives for using deallocated memory here. - ANALYZER_SUPPRESS({ - data = get_cb_data_by_channel_locked(registry, channel); - if (data == NULL) { - return EINVAL; - } + data = get_cb_data_by_channel_locked(registry, channel); + if (data == NULL) { + return EINVAL; + } + + list_del(&data->entry); + + // Analyzer thinks get_cb_data_by_channel might still return our data + // after list_del and emits a "use-after-free" warning. + // assert()s can change the assumptions of the analyzer, so we use them here. +#ifdef DEBUG + list_for_each_entry(struct platch_obj_cb_data, data_iter, ®istry->callbacks, entry) { + ASSUME(data_iter != data); + } +#endif - list_del(&data->entry); - free(data->channel); - free(data); - }); + free(data->channel); + free(data); return 0; } diff --git a/src/util/list.h b/src/util/list.h index a2ed892e..28440cd8 100644 --- a/src/util/list.h +++ b/src/util/list.h @@ -93,12 +93,9 @@ static inline void list_replace(struct list_head *from, struct list_head *to) { } static inline void list_del(struct list_head *item) { - // We need this to suppress a false positive `use-after-free` coming from pluginregistry.c. - ANALYZER_SUPPRESS({ - item->prev->next = item->next; - item->next->prev = item->prev; - item->prev = item->next = NULL; - }); + item->prev->next = item->next; + item->next->prev = item->prev; + item->prev = item->next = NULL; } static inline void list_delinit(struct list_head *item) {