spa: allow spa_dict.items to be NULL for zero items

For dicts with zero items, don't dereference dict->items, or give it to
qsort or bsearch where passing in NULL is not allowed.
This commit is contained in:
Pauli Virtanen 2022-01-20 19:55:02 +02:00
parent 5a72e2769a
commit 870cd0136a

View file

@ -74,8 +74,9 @@ static inline int spa_dict_item_compare(const void *i1, const void *i2)
static inline void spa_dict_qsort(struct spa_dict *dict)
{
qsort((void*)dict->items, dict->n_items, sizeof(struct spa_dict_item),
spa_dict_item_compare);
if (dict->n_items > 0)
qsort((void*)dict->items, dict->n_items, sizeof(struct spa_dict_item),
spa_dict_item_compare);
SPA_FLAG_SET(dict->flags, SPA_DICT_FLAG_SORTED);
}
@ -84,7 +85,8 @@ static inline const struct spa_dict_item *spa_dict_lookup_item(const struct spa_
{
const struct spa_dict_item *item;
if (SPA_FLAG_IS_SET(dict->flags, SPA_DICT_FLAG_SORTED)) {
if (SPA_FLAG_IS_SET(dict->flags, SPA_DICT_FLAG_SORTED) &&
dict->n_items > 0) {
struct spa_dict_item k = SPA_DICT_ITEM_INIT(key, NULL);
item = (const struct spa_dict_item *)bsearch(&k,
(const void *) dict->items, dict->n_items,