From 62f5aa42aada8f3a6bebbcd0bd87f79c37334c9c Mon Sep 17 00:00:00 2001 From: James Raphael Tiovalen Date: Wed, 14 Jun 2023 02:34:38 +0800 Subject: [PATCH] shash, simap, smap: Add assertions to `*_count` functions. This commit adds assertions in the functions `shash_count`, `simap_count`, and `smap_count` to ensure that the corresponding input struct pointer is not NULL. This ensures that if the return values of `shash_sort`, `simap_sort`, or `smap_sort` are NULL, then the following for loops would not attempt to access the pointer, which might result in segmentation faults or undefined behavior. Reviewed-by: Simon Horman Acked-by: Eelco Chaudron Signed-off-by: James Raphael Tiovalen Signed-off-by: Ilya Maximets --- lib/shash.c | 2 ++ lib/simap.c | 2 ++ lib/smap.c | 1 + 3 files changed, 5 insertions(+) diff --git a/lib/shash.c b/lib/shash.c index a7b2c645829..2bfc8eb507f 100644 --- a/lib/shash.c +++ b/lib/shash.c @@ -17,6 +17,7 @@ #include #include "openvswitch/shash.h" #include "hash.h" +#include "util.h" static struct shash_node *shash_find__(const struct shash *, const char *name, size_t name_len, @@ -100,6 +101,7 @@ shash_is_empty(const struct shash *shash) size_t shash_count(const struct shash *shash) { + ovs_assert(shash); return hmap_count(&shash->map); } diff --git a/lib/simap.c b/lib/simap.c index 0ee08d74d52..1c01d4ebe22 100644 --- a/lib/simap.c +++ b/lib/simap.c @@ -17,6 +17,7 @@ #include #include "simap.h" #include "hash.h" +#include "util.h" static size_t hash_name(const char *, size_t length); static struct simap_node *simap_find__(const struct simap *, @@ -84,6 +85,7 @@ simap_is_empty(const struct simap *simap) size_t simap_count(const struct simap *simap) { + ovs_assert(simap); return hmap_count(&simap->map); } diff --git a/lib/smap.c b/lib/smap.c index 47fb3450201..122adca2717 100644 --- a/lib/smap.c +++ b/lib/smap.c @@ -300,6 +300,7 @@ smap_is_empty(const struct smap *smap) size_t smap_count(const struct smap *smap) { + ovs_assert(smap); return hmap_count(&smap->map); }