Re: [PATCH] libsepol: avoid unnecessary memset(3) calls in hashtab

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Nov 29, 2024 at 6:40 AM Christian Göttsche
<cgoettsche@xxxxxxxxxxxxx> wrote:
>
> From: Christian Göttsche <cgzones@xxxxxxxxxxxxxx>
>
> Use struct initialization with designators to skip unnecessary memset(3)
> calls.  Since libsepol is not a security boundary uninitialized padding
> is not a concern.
>
> Also drop the dead assignment of a region to be free'd in the next line.
>
> Signed-off-by: Christian Göttsche <cgzones@xxxxxxxxxxxxxx>

Acked-by: James Carter <jwcart2@xxxxxxxxx>

> ---
>  libsepol/src/hashtab.c | 23 ++++++++++++-----------
>  1 file changed, 12 insertions(+), 11 deletions(-)
>
> diff --git a/libsepol/src/hashtab.c b/libsepol/src/hashtab.c
> index 399582b1..4c658588 100644
> --- a/libsepol/src/hashtab.c
> +++ b/libsepol/src/hashtab.c
> @@ -48,12 +48,14 @@ hashtab_t hashtab_create(unsigned int (*hash_value) (hashtab_t h,
>         if (p == NULL)
>                 return p;
>
> -       memset(p, 0, sizeof(hashtab_val_t));
> -       p->size = size;
> -       p->nel = 0;
> -       p->hash_value = hash_value;
> -       p->keycmp = keycmp;
> -       p->htable = (hashtab_ptr_t *) calloc(size, sizeof(hashtab_ptr_t));
> +       *p = (hashtab_val_t) {
> +               .size = size,
> +               .nel = 0,
> +               .hash_value = hash_value,
> +               .keycmp = keycmp,
> +               .htable = (hashtab_ptr_t *) calloc(size, sizeof(hashtab_ptr_t)),
> +       };
> +
>         if (p->htable == NULL) {
>                 free(p);
>                 return NULL;
> @@ -127,9 +129,10 @@ int hashtab_insert(hashtab_t h, hashtab_key_t key, hashtab_datum_t datum)
>         newnode = (hashtab_ptr_t) malloc(sizeof(hashtab_node_t));
>         if (newnode == NULL)
>                 return SEPOL_ENOMEM;
> -       memset(newnode, 0, sizeof(struct hashtab_node));
> -       newnode->key = key;
> -       newnode->datum = datum;
> +       *newnode = (hashtab_node_t) {
> +               .key = key,
> +               .datum = datum,
> +       };
>         if (prev) {
>                 newnode->next = prev->next;
>                 prev->next = newnode;
> @@ -223,8 +226,6 @@ void hashtab_destroy(hashtab_t h)
>         }
>
>         free(h->htable);
> -       h->htable = NULL;
> -
>         free(h);
>  }
>
> --
> 2.45.2
>
>





[Index of Archives]     [Selinux Refpolicy]     [Linux SGX]     [Fedora Users]     [Fedora Desktop]     [Yosemite Photos]     [Yosemite Camping]     [Yosemite Campsites]     [KDE Users]     [Gnome Users]

  Powered by Linux