Skip to content

Commit

Permalink
libsepol: avoid unnecessary memset(3) calls in hashtab
Browse files Browse the repository at this point in the history
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@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
  • Loading branch information
cgzones authored and jwcart2 committed Dec 4, 2024
1 parent 2db6d12 commit c28d920
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions libsepol/src/hashtab.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -223,8 +226,6 @@ void hashtab_destroy(hashtab_t h)
}

free(h->htable);
h->htable = NULL;

free(h);
}

Expand Down

0 comments on commit c28d920

Please sign in to comment.