diff mbox series

shared: fix warning reported by UBSan

Message ID 20230518091419.53038-1-dmantipov@yandex.ru (mailing list archive)
State New, archived
Headers show
Series shared: fix warning reported by UBSan | expand

Commit Message

Dmitry Antipov May 18, 2023, 9:14 a.m. UTC
Fix the following warning reported by UBSan (as of gcc-13.1.1):

shared/hash.c:244:35: runtime error: null pointer passed as
argument 2, which is declared to never be null

i.e. avoid passing {NULL, 0} array to bsearch().

Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
---
 shared/hash.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

Comments

Christophe Leroy May 19, 2023, 6:58 a.m. UTC | #1
Le 18/05/2023 à 11:14, Dmitry Antipov a écrit :
> [Vous ne recevez pas souvent de courriers de dmantipov@yandex.ru. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]
> 
> Fix the following warning reported by UBSan (as of gcc-13.1.1):
> 
> shared/hash.c:244:35: runtime error: null pointer passed as
> argument 2, which is declared to never be null
> 
> i.e. avoid passing {NULL, 0} array to bsearch().

The above should be the subject of the patch.

> 
> Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
> ---
>   shared/hash.c | 12 ++++++------
>   1 file changed, 6 insertions(+), 6 deletions(-)
> 
> diff --git a/shared/hash.c b/shared/hash.c
> index 7fe3f80..0e09c99 100644
> --- a/shared/hash.c
> +++ b/shared/hash.c
> @@ -241,12 +241,12 @@ void *hash_find(const struct hash *hash, const char *key)
>                  .key = key,
>                  .value = NULL
>          };
> -       const struct hash_entry *entry = bsearch(
> -               &se, bucket->entries, bucket->used,
> -               sizeof(struct hash_entry), hash_entry_cmp);
> -       if (entry == NULL)
> -               return NULL;
> -       return (void *)entry->value;
> +       const struct hash_entry *entry =
> +               (bucket->entries ?
> +                bsearch(&se, bucket->entries, bucket->used,
> +                        sizeof(struct hash_entry), hash_entry_cmp) :
> +                NULL);

Too complicated, not easy to read.

> +       return entry ? (void *)entry->value : NULL;

Previous form was more readable.

>   }
> 
>   int hash_del(struct hash *hash, const char *key)
> --
> 2.40.1
>
diff mbox series

Patch

diff --git a/shared/hash.c b/shared/hash.c
index 7fe3f80..0e09c99 100644
--- a/shared/hash.c
+++ b/shared/hash.c
@@ -241,12 +241,12 @@  void *hash_find(const struct hash *hash, const char *key)
 		.key = key,
 		.value = NULL
 	};
-	const struct hash_entry *entry = bsearch(
-		&se, bucket->entries, bucket->used,
-		sizeof(struct hash_entry), hash_entry_cmp);
-	if (entry == NULL)
-		return NULL;
-	return (void *)entry->value;
+	const struct hash_entry *entry =
+		(bucket->entries ?
+		 bsearch(&se, bucket->entries, bucket->used,
+			 sizeof(struct hash_entry), hash_entry_cmp) :
+		 NULL);
+	return entry ? (void *)entry->value : NULL;
 }
 
 int hash_del(struct hash *hash, const char *key)