Skip to content

Commit

Permalink
try another source code permutation to fix analyzer warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
ardera committed Sep 16, 2024
1 parent 1faa70f commit e309e22
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
37 changes: 21 additions & 16 deletions src/pluginregistry.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, &registry->callbacks, entry) {
if (streq(data->channel, channel)) {
return data;
}
list_for_each_entry(struct platch_obj_cb_data, data, &registry->callbacks, entry) {
if (streq(data->channel, channel)) {
return data;
}
});
}

return NULL;
}
Expand Down Expand Up @@ -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, &registry->callbacks, entry) {
ASSUME(data_iter != data);
}
#endif

list_del(&data->entry);
free(data->channel);
free(data);
});
free(data->channel);
free(data);

return 0;
}
Expand Down
9 changes: 3 additions & 6 deletions src/util/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit e309e22

Please sign in to comment.