Skip to content

Commit

Permalink
shash, simap, smap: Add assertions to *_count functions.
Browse files Browse the repository at this point in the history
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 <[email protected]>
Acked-by: Eelco Chaudron <[email protected]>
Signed-off-by: James Raphael Tiovalen <[email protected]>
Signed-off-by: Ilya Maximets <[email protected]>
  • Loading branch information
jamestiotio authored and igsilya committed Jul 13, 2023
1 parent a5fdc45 commit 62f5aa4
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/shash.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <config.h>
#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,
Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 2 additions & 0 deletions lib/simap.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <config.h>
#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 *,
Expand Down Expand Up @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions lib/smap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 62f5aa4

Please sign in to comment.