From 9593f953eef1a33f102a026c1f2269cdf4d39b5d Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Fri, 8 Jul 2022 17:36:32 +0200 Subject: [PATCH 1/2] hash: add hash_list_idx() --- include/re_hash.h | 1 + src/hash/hash.c | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/include/re_hash.h b/include/re_hash.h index 8858dbcb2..3e1f6bc76 100644 --- a/include/re_hash.h +++ b/include/re_hash.h @@ -15,6 +15,7 @@ void hash_unlink(struct le *le); struct le *hash_lookup(const struct hash *h, uint32_t key, list_apply_h *ah, void *arg); struct le *hash_apply(const struct hash *h, list_apply_h *ah, void *arg); +struct list *hash_list_idx(const struct hash *h, uint32_t i); struct list *hash_list(const struct hash *h, uint32_t key); uint32_t hash_bsize(const struct hash *h); void hash_flush(struct hash *h); diff --git a/src/hash/hash.c b/src/hash/hash.c index 45aa11e32..a66d555e8 100644 --- a/src/hash/hash.c +++ b/src/hash/hash.c @@ -140,7 +140,24 @@ struct le *hash_apply(const struct hash *h, list_apply_h *ah, void *arg) /** - * Return bucket list for a given index + * Return bucket list for a given bucket index + * + * @param h Hashmap table + * @param i Bucket index + * + * @return Bucket list if valid input, otherwise NULL + */ +struct list *hash_list_idx(const struct hash *h, uint32_t i) +{ + if (i >= h->bsize) + return NULL; + + return h ? &h->bucket[i] : NULL; +} + + +/** + * Return bucket list for a given key * * @param h Hashmap table * @param key Hash key From 11dec093bb9963a9b6c9d1c15e71c10a0c9156ef Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Fri, 8 Jul 2022 17:38:56 +0200 Subject: [PATCH 2/2] fix hash null check --- src/hash/hash.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hash/hash.c b/src/hash/hash.c index a66d555e8..96ee1173d 100644 --- a/src/hash/hash.c +++ b/src/hash/hash.c @@ -149,10 +149,10 @@ struct le *hash_apply(const struct hash *h, list_apply_h *ah, void *arg) */ struct list *hash_list_idx(const struct hash *h, uint32_t i) { - if (i >= h->bsize) + if (!h || i >= h->bsize) return NULL; - return h ? &h->bucket[i] : NULL; + return &h->bucket[i]; }