From 870cd0136aac3b9412f08d75f2a53a683e267f3e Mon Sep 17 00:00:00 2001 From: Pauli Virtanen Date: Thu, 20 Jan 2022 19:55:02 +0200 Subject: [PATCH] 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. --- spa/include/spa/utils/dict.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/spa/include/spa/utils/dict.h b/spa/include/spa/utils/dict.h index 2ba95e0a9..558c6fd01 100644 --- a/spa/include/spa/utils/dict.h +++ b/spa/include/spa/utils/dict.h @@ -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,