diff mbox series

[ovs-dev,v7,3/8] shash, simap, smap: Return 0 if struct is NULL or hmap is empty

Message ID 20230415152155.762025-4-jamestiotio@gmail.com
State Changes Requested, archived
Headers show
Series treewide: Fix multiple Coverity defects | expand

Checks

Context Check Description
ovsrobot/apply-robot success apply and check: success
ovsrobot/github-robot-_Build_and_Test success github build: passed
ovsrobot/intel-ovs-compilation success test: success

Commit Message

James Raphael Tiovalen April 15, 2023, 3:21 p.m. UTC
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 <jamestiotio@gmail.com>
---
 lib/shash.c | 4 ++++
 lib/simap.c | 4 ++++
 lib/smap.c  | 4 ++++
 3 files changed, 12 insertions(+)

Comments

Simon Horman April 24, 2023, 11:47 a.m. UTC | #1
On Sat, Apr 15, 2023 at 11:21:50PM +0800, 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 <jamestiotio@gmail.com>

Reviewed-by: Simon Horman <simon.horman@corigine.com>
Ilya Maximets April 24, 2023, 8:17 p.m. UTC | #2
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 <jamestiotio@gmail.com>
> ---
>  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);
>  }
>
James Raphael Tiovalen May 1, 2023, 3:01 p.m. UTC | #3
On Tue, Apr 25, 2023 at 4:17 AM Ilya Maximets <i.maximets@ovn.org> wrote:
>
> 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.
>

Got it. In that case, I can just `ovs_assert()` that `shash`, `simap`,
and `smap` are not NULL.

Best regards,
James Raphael Tiovalen
diff mbox series

Patch

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)) {
+        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);
 }