On 4/15/23 17:21, James Raphael Tiovalen wrote:
> This commit adds a check in the functions `shash_count`, `simap_count`,
> and `smap_count` to return 0 if the corresponding input struct pointer
> is NULL or if the corresponding hmap is empty.
> 
> 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.
> 
> Signed-off-by: James Raphael Tiovalen <[email protected]>
> ---
>  lib/shash.c | 4 ++++
>  lib/simap.c | 4 ++++
>  lib/smap.c  | 4 ++++
>  3 files changed, 12 insertions(+)
> 
> diff --git a/lib/shash.c b/lib/shash.c
> index a7b2c6458..2b1d55901 100644
> --- a/lib/shash.c
> +++ b/lib/shash.c
> @@ -100,6 +100,10 @@ shash_is_empty(const struct shash *shash)
>  size_t
>  shash_count(const struct shash *shash)
>  {
> +    if (!shash || shash_is_empty(shash)) {

shash_is_empty(shash) is the same as hmap_count(&shash->map) == 0.
There should be no need to check, since we're returning the result
of hmap_count() below anyway.

Same for other structures.  And these should, likely, be just assertions.

> +        return 0;
> +    }
> +
>      return hmap_count(&shash->map);
>  }
>  
> diff --git a/lib/simap.c b/lib/simap.c
> index 0ee08d74d..48c331dfe 100644
> --- a/lib/simap.c
> +++ b/lib/simap.c
> @@ -84,6 +84,10 @@ simap_is_empty(const struct simap *simap)
>  size_t
>  simap_count(const struct simap *simap)
>  {
> +    if (!simap || simap_is_empty(simap)) {
> +        return 0;
> +    }
> +
>      return hmap_count(&simap->map);
>  }
>  
> diff --git a/lib/smap.c b/lib/smap.c
> index c1633e2a1..46e87b0ab 100644
> --- a/lib/smap.c
> +++ b/lib/smap.c
> @@ -300,6 +300,10 @@ smap_is_empty(const struct smap *smap)
>  size_t
>  smap_count(const struct smap *smap)
>  {
> +    if (!smap || smap_is_empty(smap)) {
> +        return 0;
> +    }
> +
>      return hmap_count(&smap->map);
>  }
>  

_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to