diff --git a/src/pipewire/array.h b/src/pipewire/array.h index 7fecb6434..144e0177d 100644 --- a/src/pipewire/array.h +++ b/src/pipewire/array.h @@ -67,6 +67,7 @@ struct pw_array { #define pw_array_first(a) ((a)->data) #define pw_array_end(a) SPA_PTROFF((a)->data, (a)->size, void) +#define pw_array_last(a, T) (((T *) pw_array_end(a)) - 1) #define pw_array_check(a,p) (SPA_PTROFF(p,sizeof(*p),void) <= pw_array_end(a)) #define pw_array_for_each(pos, array) \ diff --git a/src/pipewire/properties.c b/src/pipewire/properties.c index a84244858..11d81c064 100644 --- a/src/pipewire/properties.c +++ b/src/pipewire/properties.c @@ -59,6 +59,17 @@ static int add_func(struct pw_properties *this, char *key, char *value) return 0; } +static void remove_item(struct pw_properties *this, struct spa_dict_item *item) +{ + struct properties *impl = SPA_CONTAINER_OF(this, struct properties, this); + const struct spa_dict_item *last = pw_array_last(&impl->items, struct spa_dict_item); + + item->key = last->key; + item->value = last->value; + impl->items.size -= sizeof(*item); + this->dict.n_items--; +} + static void clear_item(struct spa_dict_item *item) { free((char *) item->key); @@ -385,7 +396,6 @@ void pw_properties_free(struct pw_properties *properties) static int do_replace(struct pw_properties *properties, const char *key, char *value, bool copy) { - struct properties *impl = SPA_CONTAINER_OF(properties, struct properties, this); struct spa_dict_item *item; if (key == NULL || key[0] == 0) @@ -403,14 +413,8 @@ static int do_replace(struct pw_properties *properties, const char *key, char *v goto exit_noupdate; if (value == NULL) { - struct spa_dict_item *last = pw_array_get_unchecked(&impl->items, - pw_array_get_len(&impl->items, struct spa_dict_item) - 1, - struct spa_dict_item); clear_item(item); - item->key = last->key; - item->value = last->value; - impl->items.size -= sizeof(struct spa_dict_item); - properties->dict.n_items--; + remove_item(properties, item); SPA_FLAG_CLEAR(properties->dict.flags, SPA_DICT_FLAG_SORTED); } else { free((char *) item->value);