diff mbox series

libsepol: avoid unnecessary memset(3) calls in hashtab

Message ID 20241128154034.23298-1-cgoettsche@seltendoof.de (mailing list archive)
State New
Headers show
Series libsepol: avoid unnecessary memset(3) calls in hashtab | expand

Commit Message

Christian Göttsche Nov. 28, 2024, 3:40 p.m. UTC
From: Christian Göttsche <cgzones@googlemail.com>

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>
---
 libsepol/src/hashtab.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
diff mbox series

Patch

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