Skip to contentSkip to conversation

checkpolicy: replace malloc()+memset() with calloc()

PATCH Superseded Kalevi Kolttonen 2 participants 8 messages May 7 – May 8 SELinux security module

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 checkpolicy: replace malloc()+memset() with calloc()

---
 checkpolicy/policy_define.c | 81 +++++++++++++------------------------
 1 file changed, 27 insertions(+), 54 deletions(-)
checkpolicy/policy_define.c +27 -54
index b73de45c..a08cdf59 100644
@@ -175,12 +175,11 @@ int define_class(void)
 		yyerror("no class name for class definition?");
 		return -1;
 	}
-	datum = (class_datum_t *) malloc(sizeof(class_datum_t));
+	datum = (class_datum_t *) calloc(1, sizeof(class_datum_t));
 	if (!datum) {
 		yyerror("out of memory");
 		goto cleanup;
 	}
-	memset(datum, 0, sizeof(class_datum_t));
 	ret = declare_symbol(SYM_CLASSES, id, datum, &value, &value);
 	switch (ret) {
 	case -3:{
@@ -354,12 +353,11 @@ int define_initial_sid(void)
 		yyerror("no sid name for SID definition?");
 		return -1;
 	}
-	newc = (ocontext_t *) malloc(sizeof(ocontext_t));
+	newc = (ocontext_t *) calloc(1, sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
 		goto bad;
 	}
-	memset(newc, 0, sizeof(ocontext_t));
 	newc->u.name = id;
 	context_init(&newc->context[0]);
 	head = policydbp->ocontexts[OCON_ISID];
@@ -579,12 +577,11 @@ int define_common_perms(void)
 		free(id);
 		return -1;
 	}
-	comdatum = (common_datum_t *) malloc(sizeof(common_datum_t));
+	comdatum = (common_datum_t *) calloc(1, sizeof(common_datum_t));
 	if (!comdatum) {
 		yyerror("out of memory");
 		goto bad;
 	}
-	memset(comdatum, 0, sizeof(common_datum_t));
 	ret = hashtab_insert(policydbp->p_commons.table,
 			     (hashtab_key_t) id, (hashtab_datum_t) comdatum);
 
@@ -603,12 +600,11 @@ int define_common_perms(void)
 	}
 	policydbp->p_commons.nprim++;
 	while ((perm = queue_remove(id_queue))) {
-		perdatum = (perm_datum_t *) malloc(sizeof(perm_datum_t));
+		perdatum = (perm_datum_t *) calloc(1, sizeof(perm_datum_t));
 		if (!perdatum) {
 			yyerror("out of memory");
 			goto bad_perm;
 		}
-		memset(perdatum, 0, sizeof(perm_datum_t));
 		perdatum->s.value = comdatum->permissions.nprim + 1;
 
 		if (perdatum->s.value > (sizeof(sepol_access_vector_t) * 8)) {
@@ -713,12 +709,11 @@ int define_av_perms(int inherits)
 		cladatum->permissions.nprim += comdatum->permissions.nprim;
 	}
 	while ((id = queue_remove(id_queue))) {
-		perdatum = (perm_datum_t *) malloc(sizeof(perm_datum_t));
+		perdatum = (perm_datum_t *) calloc(1, sizeof(perm_datum_t));
 		if (!perdatum) {
 			yyerror("out of memory");
 			goto bad;
 		}
-		memset(perdatum, 0, sizeof(perm_datum_t));
 		perdatum->s.value = ++cladatum->permissions.nprim;
 
 		if (perdatum->s.value > (sizeof(sepol_access_vector_t) * 8)) {
@@ -1330,13 +1325,12 @@ static int add_aliases_to_type(type_datum_t * type)
 			free(id);
 			return -1;
 		}
-		aliasdatum = (type_datum_t *) malloc(sizeof(type_datum_t));
+		aliasdatum = (type_datum_t *) calloc(1, sizeof(type_datum_t));
 		if (!aliasdatum) {
 			free(id);
 			yyerror("Out of memory!");
 			return -1;
 		}
-		memset(aliasdatum, 0, sizeof(type_datum_t));
 		aliasdatum->s.value = type->s.value;
 
 		ret = declare_symbol(SYM_TYPES, id, aliasdatum,
@@ -1890,13 +1884,12 @@ int define_bool_tunable(int is_tunable)
 		free(id);
 		return -1;
 	}
-	datum = (cond_bool_datum_t *) malloc(sizeof(cond_bool_datum_t));
+	datum = (cond_bool_datum_t *) calloc(1, sizeof(cond_bool_datum_t));
 	if (!datum) {
 		yyerror("out of memory");
 		free(id);
 		return -1;
 	}
-	memset(datum, 0, sizeof(cond_bool_datum_t));
 	if (is_tunable)
 		datum->flags |= COND_BOOL_FLAGS_TUNABLE;
 	ret = declare_symbol(SYM_BOOLS, id, datum, &value, &value);
@@ -3343,12 +3336,11 @@ int define_role_trans(int class_specified)
 					}
 				}
 
-				tr = malloc(sizeof(struct role_trans));
+				tr = calloc(1, sizeof(struct role_trans));
 				if (!tr) {
 					yyerror("out of memory");
 					goto bad;
 				}
-				memset(tr, 0, sizeof(struct role_trans));
 				tr->role = i + 1;
 				tr->type = j + 1;
 				tr->tclass = k + 1;
@@ -3359,12 +3351,11 @@ int define_role_trans(int class_specified)
 		}
 	}
 	/* Now add the real rule */
-	rule = malloc(sizeof(struct role_trans_rule));
+	rule = calloc(1, sizeof(struct role_trans_rule));
 	if (!rule) {
 		yyerror("out of memory");
 		goto bad;
 	}
-	memset(rule, 0, sizeof(struct role_trans_rule));
 	rule->roles = roles;
 	rule->types = types;
 	rule->classes = e_classes;
@@ -3731,13 +3722,12 @@ int define_constraint(constraint_expr_t * expr)
 			free(id);
 			goto bad;
 		}
-		node = malloc(sizeof(struct constraint_node));
+		node = calloc(1, sizeof(struct constraint_node));
 		if (!node) {
 			yyerror("out of memory");
 			free(node);
 			goto bad;
 		}
-		memset(node, 0, sizeof(constraint_node_t));
 		if (useexpr) {
 			node->expr = expr;
 			useexpr = 0;
@@ -3892,13 +3882,12 @@ int define_validatetrans(constraint_expr_t * expr)
 			goto bad;
 		}
 
-		node = malloc(sizeof(struct constraint_node));
+		node = calloc(1, sizeof(struct constraint_node));
 		if (!node) {
 			yyerror("out of memory");
 			free(id);
 			goto bad;
 		}
-		memset(node, 0, sizeof(constraint_node_t));
 		if (useexpr) {
 			node->expr = expr;
 			useexpr = 0;
@@ -4209,12 +4198,11 @@ cond_expr_t *define_cond_expr(uint32_t expr_type, void *arg1, void *arg2)
 	}
 
 	/* create a new expression struct */
-	expr = malloc(sizeof(struct cond_expr));
+	expr = calloc(1, sizeof(struct cond_expr));
 	if (!expr) {
 		yyerror("out of memory");
 		return NULL;
 	}
-	memset(expr, 0, sizeof(cond_expr_t));
 	expr->expr_type = expr_type;
 
 	/* create the type asked for */
@@ -4864,12 +4852,11 @@ int define_fs_context(unsigned int major, unsigned int minor)
 		return 0;
 	}
 
-	newc = (ocontext_t *) malloc(sizeof(ocontext_t));
+	newc = (ocontext_t *) calloc(1, sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
 		return -1;
 	}
-	memset(newc, 0, sizeof(ocontext_t));
 
 	newc->u.name = (char *)malloc(6);
 	if (!newc->u.name) {
@@ -4924,12 +4911,11 @@ int define_pirq_context(unsigned int pirq)
 		return 0;
 	}
 
-	newc = malloc(sizeof(ocontext_t));
+	newc = calloc(1, sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
 		return -1;
 	}
-	memset(newc, 0, sizeof(ocontext_t));
 
 	newc->u.pirq = pirq;
 
@@ -4975,12 +4961,11 @@ int define_iomem_context(uint64_t low, uint64_t high)
 		return 0;
 	}
 
-	newc = malloc(sizeof(ocontext_t));
+	newc = calloc(1, sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
 		return -1;
 	}
-	memset(newc, 0, sizeof(ocontext_t));
 
 	newc->u.iomem.low_iomem  = low;
 	newc->u.iomem.high_iomem = high;
@@ -5036,12 +5021,11 @@ int define_ioport_context(unsigned long low, unsigned long high)
 		return 0;
 	}
 
-	newc = malloc(sizeof(ocontext_t));
+	newc = calloc(1, sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
 		return -1;
 	}
-	memset(newc, 0, sizeof(ocontext_t));
 
 	newc->u.ioport.low_ioport  = low;
 	newc->u.ioport.high_ioport = high;
@@ -5097,12 +5081,11 @@ int define_pcidevice_context(unsigned long device)
 		return 0;
 	}
 
-	newc = malloc(sizeof(ocontext_t));
+	newc = calloc(1, sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
 		return -1;
 	}
-	memset(newc, 0, sizeof(ocontext_t));
 
 	newc->u.device = device;
 
@@ -5150,12 +5133,11 @@ int define_devicetree_context(void)
 		return 0;
 	}
 
-	newc = malloc(sizeof(ocontext_t));
+	newc = calloc(1, sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
 		return -1;
 	}
-	memset(newc, 0, sizeof(ocontext_t));
 
 	newc->u.name = (char *)queue_remove(id_queue);
 	if (!newc->u.name) {
@@ -5208,12 +5190,11 @@ int define_port_context(unsigned int low, unsigned int high)
 		return 0;
 	}
 
-	newc = malloc(sizeof(ocontext_t));
+	newc = calloc(1, sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
 		return -1;
 	}
-	memset(newc, 0, sizeof(ocontext_t));
 
 	id = (char *)queue_remove(id_queue);
 	if (!id) {
@@ -5301,12 +5282,11 @@ int define_ibpkey_context(unsigned int low, unsigned int high)
 		return 0;
 	}
 
-	newc = malloc(sizeof(*newc));
+	newc = calloc(1, sizeof(*newc));
 	if (!newc) {
 		yyerror("out of memory");
 		return -1;
 	}
-	memset(newc, 0, sizeof(*newc));
 
 	id = queue_remove(id_queue);
 	if (!id) {
@@ -5411,12 +5391,11 @@ int define_ibendport_context(unsigned int port)
 		return -1;
 	}
 
-	newc = malloc(sizeof(*newc));
+	newc = calloc(1, sizeof(*newc));
 	if (!newc) {
 		yyerror("out of memory");
 		return -1;
 	}
-	memset(newc, 0, sizeof(*newc));
 
 	newc->u.ibendport.dev_name = queue_remove(id_queue);
 	if (!newc->u.ibendport.dev_name) {
@@ -5484,12 +5463,11 @@ int define_netif_context(void)
 		return 0;
 	}
 
-	newc = (ocontext_t *) malloc(sizeof(ocontext_t));
+	newc = (ocontext_t *) calloc(1, sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
 		return -1;
 	}
-	memset(newc, 0, sizeof(ocontext_t));
 
 	newc->u.name = (char *)queue_remove(id_queue);
 	if (!newc->u.name) {
@@ -5615,13 +5593,12 @@ int define_ipv4_node_context(void)
 		yywarn("host bits in ipv4 address set");
 	}
 
-	newc = malloc(sizeof(ocontext_t));
+	newc = calloc(1, sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
 		return -1;
 	}
 
-	memset(newc, 0, sizeof(ocontext_t));
 	newc->u.node.addr = addr.s_addr;
 	newc->u.node.mask = mask.s_addr;
 
@@ -5864,13 +5841,12 @@ int define_ipv6_node_context(void)
 		yywarn("host bits in ipv6 address set");
 	}
 
-	newc = malloc(sizeof(ocontext_t));
+	newc = calloc(1, sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
 		return -1;
 	}
 
-	memset(newc, 0, sizeof(ocontext_t));
 	memcpy(&newc->u.node6.addr[0], &addr.s6_addr[0], 16);
 	memcpy(&newc->u.node6.mask[0], &mask.s6_addr[0], 16);
 
@@ -5975,12 +5951,11 @@ int define_fs_use(int behavior)
 		return 0;
 	}
 
-	newc = (ocontext_t *) malloc(sizeof(ocontext_t));
+	newc = (ocontext_t *) calloc(1, sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
 		return -1;
 	}
-	memset(newc, 0, sizeof(ocontext_t));
 
 	newc->u.name = (char *)queue_remove(id_queue);
 	if (!newc->u.name) {
@@ -6043,12 +6018,11 @@ static int define_genfs_context_helper(char *fstype, int has_type)
 	}
 
 	if (!genfs || strcmp(fstype, genfs->fstype)) {
-		newgenfs = malloc(sizeof(struct genfs));
+		newgenfs = calloc(1, sizeof(struct genfs));
 		if (!newgenfs) {
 			yyerror("out of memory");
 			return -1;
 		}
-		memset(newgenfs, 0, sizeof(struct genfs));
 		newgenfs->fstype = fstype;
 		newgenfs->next = genfs;
 		if (genfs_p)
@@ -6061,12 +6035,11 @@ static int define_genfs_context_helper(char *fstype, int has_type)
 		fstype = NULL;
 	}
 
-	newc = (ocontext_t *) malloc(sizeof(ocontext_t));
+	newc = (ocontext_t *) calloc(1, sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
 		return -1;
 	}
-	memset(newc, 0, sizeof(ocontext_t));
 
 	newc->u.name = (char *)queue_remove(id_queue);
 	if (!newc->u.name)
Signature · 1 line
2.54.0
#1 lore raw

On Thu May 7, 2026 at 10:38 PM BST, Kalevi Kolttonen wrote:

This overall looks like a good change. I have a few nit suggestions.

Quoted 10 lines
---
 checkpolicy/policy_define.c | 81 +++++++++++++------------------------
 1 file changed, 27 insertions(+), 54 deletions(-)
checkpolicy/policy_define.c +0 -0
index b73de45c..a08cdf59 100644

checkpolicy/policy_define.c int define_class(void) · line 178

Quoted 5 lines
checkpolicy/policy_define.c +1 -1
@@ -175,12 +175,11 @@ int define_class(void)
 		yyerror("no class name for class definition?");
 		return -1;
 	}
-	datum = (class_datum_t *) malloc(sizeof(class_datum_t));
+	datum = (class_datum_t *) calloc(1, sizeof(class_datum_t));

What is the point of the cast from void * here? I do realize that this is not something that you introduced, but given that said cast is not consistently applied, even in this file, and is a bit pointless, I'd personally say we may as well just drop it whilst we're at it.

Also, I am of the opinion that it is preferred to use *foo over sizeof(type), i.e. in this case *datum instead of sizeof(class_datum_t). Whilst this _is_ a purely stylistic change, it does carry some practical benifits too. Namely, if the type of datum changes in the future, one can't accidently forget to update the call to calloc()!

Quoted 378 lines
checkpolicy/policy_define.c +26 -53
 	if (!datum) {
 		yyerror("out of memory");
 		goto cleanup;
 	}
-	memset(datum, 0, sizeof(class_datum_t));
 	ret = declare_symbol(SYM_CLASSES, id, datum, &value, &value);
 	switch (ret) {
 	case -3:{
@@ -354,12 +353,11 @@ int define_initial_sid(void)
 		yyerror("no sid name for SID definition?");
 		return -1;
 	}
-	newc = (ocontext_t *) malloc(sizeof(ocontext_t));
+	newc = (ocontext_t *) calloc(1, sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
 		goto bad;
 	}
-	memset(newc, 0, sizeof(ocontext_t));
 	newc->u.name = id;
 	context_init(&newc->context[0]);
 	head = policydbp->ocontexts[OCON_ISID];
@@ -579,12 +577,11 @@ int define_common_perms(void)
 		free(id);
 		return -1;
 	}
-	comdatum = (common_datum_t *) malloc(sizeof(common_datum_t));
+	comdatum = (common_datum_t *) calloc(1, sizeof(common_datum_t));
 	if (!comdatum) {
 		yyerror("out of memory");
 		goto bad;
 	}
-	memset(comdatum, 0, sizeof(common_datum_t));
 	ret = hashtab_insert(policydbp->p_commons.table,
 			     (hashtab_key_t) id, (hashtab_datum_t) comdatum);
 
@@ -603,12 +600,11 @@ int define_common_perms(void)
 	}
 	policydbp->p_commons.nprim++;
 	while ((perm = queue_remove(id_queue))) {
-		perdatum = (perm_datum_t *) malloc(sizeof(perm_datum_t));
+		perdatum = (perm_datum_t *) calloc(1, sizeof(perm_datum_t));
 		if (!perdatum) {
 			yyerror("out of memory");
 			goto bad_perm;
 		}
-		memset(perdatum, 0, sizeof(perm_datum_t));
 		perdatum->s.value = comdatum->permissions.nprim + 1;
 
 		if (perdatum->s.value > (sizeof(sepol_access_vector_t) * 8)) {
@@ -713,12 +709,11 @@ int define_av_perms(int inherits)
 		cladatum->permissions.nprim += comdatum->permissions.nprim;
 	}
 	while ((id = queue_remove(id_queue))) {
-		perdatum = (perm_datum_t *) malloc(sizeof(perm_datum_t));
+		perdatum = (perm_datum_t *) calloc(1, sizeof(perm_datum_t));
 		if (!perdatum) {
 			yyerror("out of memory");
 			goto bad;
 		}
-		memset(perdatum, 0, sizeof(perm_datum_t));
 		perdatum->s.value = ++cladatum->permissions.nprim;
 
 		if (perdatum->s.value > (sizeof(sepol_access_vector_t) * 8)) {
@@ -1330,13 +1325,12 @@ static int add_aliases_to_type(type_datum_t * type)
 			free(id);
 			return -1;
 		}
-		aliasdatum = (type_datum_t *) malloc(sizeof(type_datum_t));
+		aliasdatum = (type_datum_t *) calloc(1, sizeof(type_datum_t));
 		if (!aliasdatum) {
 			free(id);
 			yyerror("Out of memory!");
 			return -1;
 		}
-		memset(aliasdatum, 0, sizeof(type_datum_t));
 		aliasdatum->s.value = type->s.value;
 
 		ret = declare_symbol(SYM_TYPES, id, aliasdatum,
@@ -1890,13 +1884,12 @@ int define_bool_tunable(int is_tunable)
 		free(id);
 		return -1;
 	}
-	datum = (cond_bool_datum_t *) malloc(sizeof(cond_bool_datum_t));
+	datum = (cond_bool_datum_t *) calloc(1, sizeof(cond_bool_datum_t));
 	if (!datum) {
 		yyerror("out of memory");
 		free(id);
 		return -1;
 	}
-	memset(datum, 0, sizeof(cond_bool_datum_t));
 	if (is_tunable)
 		datum->flags |= COND_BOOL_FLAGS_TUNABLE;
 	ret = declare_symbol(SYM_BOOLS, id, datum, &value, &value);
@@ -3343,12 +3336,11 @@ int define_role_trans(int class_specified)
 					}
 				}
 
-				tr = malloc(sizeof(struct role_trans));
+				tr = calloc(1, sizeof(struct role_trans));
 				if (!tr) {
 					yyerror("out of memory");
 					goto bad;
 				}
-				memset(tr, 0, sizeof(struct role_trans));
 				tr->role = i + 1;
 				tr->type = j + 1;
 				tr->tclass = k + 1;
@@ -3359,12 +3351,11 @@ int define_role_trans(int class_specified)
 		}
 	}
 	/* Now add the real rule */
-	rule = malloc(sizeof(struct role_trans_rule));
+	rule = calloc(1, sizeof(struct role_trans_rule));
 	if (!rule) {
 		yyerror("out of memory");
 		goto bad;
 	}
-	memset(rule, 0, sizeof(struct role_trans_rule));
 	rule->roles = roles;
 	rule->types = types;
 	rule->classes = e_classes;
@@ -3731,13 +3722,12 @@ int define_constraint(constraint_expr_t * expr)
 			free(id);
 			goto bad;
 		}
-		node = malloc(sizeof(struct constraint_node));
+		node = calloc(1, sizeof(struct constraint_node));
 		if (!node) {
 			yyerror("out of memory");
 			free(node);
 			goto bad;
 		}
-		memset(node, 0, sizeof(constraint_node_t));
 		if (useexpr) {
 			node->expr = expr;
 			useexpr = 0;
@@ -3892,13 +3882,12 @@ int define_validatetrans(constraint_expr_t * expr)
 			goto bad;
 		}
 
-		node = malloc(sizeof(struct constraint_node));
+		node = calloc(1, sizeof(struct constraint_node));
 		if (!node) {
 			yyerror("out of memory");
 			free(id);
 			goto bad;
 		}
-		memset(node, 0, sizeof(constraint_node_t));
 		if (useexpr) {
 			node->expr = expr;
 			useexpr = 0;
@@ -4209,12 +4198,11 @@ cond_expr_t *define_cond_expr(uint32_t expr_type, void *arg1, void *arg2)
 	}
 
 	/* create a new expression struct */
-	expr = malloc(sizeof(struct cond_expr));
+	expr = calloc(1, sizeof(struct cond_expr));
 	if (!expr) {
 		yyerror("out of memory");
 		return NULL;
 	}
-	memset(expr, 0, sizeof(cond_expr_t));
 	expr->expr_type = expr_type;
 
 	/* create the type asked for */
@@ -4864,12 +4852,11 @@ int define_fs_context(unsigned int major, unsigned int minor)
 		return 0;
 	}
 
-	newc = (ocontext_t *) malloc(sizeof(ocontext_t));
+	newc = (ocontext_t *) calloc(1, sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
 		return -1;
 	}
-	memset(newc, 0, sizeof(ocontext_t));
 
 	newc->u.name = (char *)malloc(6);
 	if (!newc->u.name) {
@@ -4924,12 +4911,11 @@ int define_pirq_context(unsigned int pirq)
 		return 0;
 	}
 
-	newc = malloc(sizeof(ocontext_t));
+	newc = calloc(1, sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
 		return -1;
 	}
-	memset(newc, 0, sizeof(ocontext_t));
 
 	newc->u.pirq = pirq;
 
@@ -4975,12 +4961,11 @@ int define_iomem_context(uint64_t low, uint64_t high)
 		return 0;
 	}
 
-	newc = malloc(sizeof(ocontext_t));
+	newc = calloc(1, sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
 		return -1;
 	}
-	memset(newc, 0, sizeof(ocontext_t));
 
 	newc->u.iomem.low_iomem  = low;
 	newc->u.iomem.high_iomem = high;
@@ -5036,12 +5021,11 @@ int define_ioport_context(unsigned long low, unsigned long high)
 		return 0;
 	}
 
-	newc = malloc(sizeof(ocontext_t));
+	newc = calloc(1, sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
 		return -1;
 	}
-	memset(newc, 0, sizeof(ocontext_t));
 
 	newc->u.ioport.low_ioport  = low;
 	newc->u.ioport.high_ioport = high;
@@ -5097,12 +5081,11 @@ int define_pcidevice_context(unsigned long device)
 		return 0;
 	}
 
-	newc = malloc(sizeof(ocontext_t));
+	newc = calloc(1, sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
 		return -1;
 	}
-	memset(newc, 0, sizeof(ocontext_t));
 
 	newc->u.device = device;
 
@@ -5150,12 +5133,11 @@ int define_devicetree_context(void)
 		return 0;
 	}
 
-	newc = malloc(sizeof(ocontext_t));
+	newc = calloc(1, sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
 		return -1;
 	}
-	memset(newc, 0, sizeof(ocontext_t));
 
 	newc->u.name = (char *)queue_remove(id_queue);
 	if (!newc->u.name) {
@@ -5208,12 +5190,11 @@ int define_port_context(unsigned int low, unsigned int high)
 		return 0;
 	}
 
-	newc = malloc(sizeof(ocontext_t));
+	newc = calloc(1, sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
 		return -1;
 	}
-	memset(newc, 0, sizeof(ocontext_t));
 
 	id = (char *)queue_remove(id_queue);
 	if (!id) {
@@ -5301,12 +5282,11 @@ int define_ibpkey_context(unsigned int low, unsigned int high)
 		return 0;
 	}
 
-	newc = malloc(sizeof(*newc));
+	newc = calloc(1, sizeof(*newc));
 	if (!newc) {
 		yyerror("out of memory");
 		return -1;
 	}
-	memset(newc, 0, sizeof(*newc));
 
 	id = queue_remove(id_queue);
 	if (!id) {
@@ -5411,12 +5391,11 @@ int define_ibendport_context(unsigned int port)
 		return -1;
 	}
 
-	newc = malloc(sizeof(*newc));
+	newc = calloc(1, sizeof(*newc));
 	if (!newc) {
 		yyerror("out of memory");
 		return -1;
 	}
-	memset(newc, 0, sizeof(*newc));
 
 	newc->u.ibendport.dev_name = queue_remove(id_queue);
 	if (!newc->u.ibendport.dev_name) {
@@ -5484,12 +5463,11 @@ int define_netif_context(void)
 		return 0;
 	}
 
-	newc = (ocontext_t *) malloc(sizeof(ocontext_t));
+	newc = (ocontext_t *) calloc(1, sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
 		return -1;
 	}
-	memset(newc, 0, sizeof(ocontext_t));
 
 	newc->u.name = (char *)queue_remove(id_queue);
 	if (!newc->u.name) {
@@ -5615,13 +5593,12 @@ int define_ipv4_node_context(void)
 		yywarn("host bits in ipv4 address set");
 	}
 
-	newc = malloc(sizeof(ocontext_t));
+	newc = calloc(1, sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
 		return -1;
 	}
 
-	memset(newc, 0, sizeof(ocontext_t));
 	newc->u.node.addr = addr.s_addr;
 	newc->u.node.mask = mask.s_addr;
 
@@ -5864,13 +5841,12 @@ int define_ipv6_node_context(void)
 		yywarn("host bits in ipv6 address set");
 	}
 
-	newc = malloc(sizeof(ocontext_t));
+	newc = calloc(1, sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
 		return -1;
 	}
 
-	memset(newc, 0, sizeof(ocontext_t));
 	memcpy(&newc->u.node6.addr[0], &addr.s6_addr[0], 16);
 	memcpy(&newc->u.node6.mask[0], &mask.s6_addr[0], 16);
 
@@ -5975,12 +5951,11 @@ int define_fs_use(int behavior)
 		return 0;
 	}
 
-	newc = (ocontext_t *) malloc(sizeof(ocontext_t));
+	newc = (ocontext_t *) calloc(1, sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
 		return -1;
 	}
-	memset(newc, 0, sizeof(ocontext_t));
 
 	newc->u.name = (char *)queue_remove(id_queue);
 	if (!newc->u.name) {
@@ -6043,12 +6018,11 @@ static int define_genfs_context_helper(char *fstype, int has_type)
 	}
 
 	if (!genfs || strcmp(fstype, genfs->fstype)) {
-		newgenfs = malloc(sizeof(struct genfs));
+		newgenfs = calloc(1, sizeof(struct genfs));
 		if (!newgenfs) {
 			yyerror("out of memory");
 			return -1;
 		}
-		memset(newgenfs, 0, sizeof(struct genfs));
 		newgenfs->fstype = fstype;
 		newgenfs->next = genfs;
 		if (genfs_p)
@@ -6061,12 +6035,11 @@ static int define_genfs_context_helper(char *fstype, int has_type)
 		fstype = NULL;
 	}
 
-	newc = (ocontext_t *) malloc(sizeof(ocontext_t));
+	newc = (ocontext_t *) calloc(1, sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
 		return -1;
 	}
-	memset(newc, 0, sizeof(ocontext_t));
 
 	newc->u.name = (char *)queue_remove(id_queue);
 	if (!newc->u.name)
#2 lore raw
Quoted 9 lines · Rahul Sandhu

On Thu May 7, 2026 at 10:38 PM BST, Kalevi Kolttonen wrote:

This overall looks like a good change. I have a few nit suggestions.

Quoted 5 lines
---
 checkpolicy/policy_define.c | 81 +++++++++++++------------------------
 1 file changed, 27 insertions(+), 54 deletions(-)
Quoted 15 lines

checkpolicy/policy_define.c int define_class(void) · line 178

Quoted 10 lines
checkpolicy/policy_define.c +1 -1
index b73de45c..a08cdf59 100644
@@ -175,12 +175,11 @@ int define_class(void)
 		yyerror("no class name for class definition?");
 		return -1;
 	}
-	datum = (class_datum_t *) malloc(sizeof(class_datum_t));
+	datum = (class_datum_t *) calloc(1, sizeof(class_datum_t));

What is the point of the cast from void * here? I do realize that this is not something that you introduced, but given that said cast is not consistently applied, even in this file, and is a bit pointless, I'd personally say we may as well just drop it whilst we're at it.

That is *exactly* what I was thinking of, too. I am no C guru but I remember learning from somewhere (maybe the old C FAQ?) that it is in general a bad habit to cast malloc() and calloc() return values because it is at best useless and when erroneous, could mask errors.

It is too late for today, but I will send PATCH v2 later.

Quoted 5 lines

Also, I am of the opinion that it is preferred to use *foo over sizeof(type), i.e. in this case *datum instead of sizeof(class_datum_t). Whilst this _is_ a purely stylistic change, it does carry some practical benifits too. Namely, if the type of datum changes in the future, one can't accidently forget to update the call to calloc()!

I know. I will fix those as well later. Thanks for your comments! So happy having removed 27 useless memset()s!

br,
KK

#3 lore raw
Quoted 26 lines · Kalevi Kolttonen
Quoted 9 lines · Rahul Sandhu

On Thu May 7, 2026 at 10:38 PM BST, Kalevi Kolttonen wrote:

This overall looks like a good change. I have a few nit suggestions.

Quoted 5 lines
---
 checkpolicy/policy_define.c | 81 +++++++++++++------------------------
 1 file changed, 27 insertions(+), 54 deletions(-)
Quoted 15 lines

checkpolicy/policy_define.c int define_class(void) · line 178

Quoted 10 lines
checkpolicy/policy_define.c +1 -1
index b73de45c..a08cdf59 100644
@@ -175,12 +175,11 @@ int define_class(void)
 		yyerror("no class name for class definition?");
 		return -1;
 	}
-	datum = (class_datum_t *) malloc(sizeof(class_datum_t));
+	datum = (class_datum_t *) calloc(1, sizeof(class_datum_t));

What is the point of the cast from void * here? I do realize that this is not something that you introduced, but given that said cast is not consistently applied, even in this file, and is a bit pointless, I'd personally say we may as well just drop it whilst we're at it.

Quoted 7 lines

That is *exactly* what I was thinking of, too. I am no C guru but I remember learning from somewhere (maybe the old C FAQ?) that it is in general a bad habit to cast malloc() and calloc() return values because it is at best useless and when erroneous, could mask errors.

It is too late for today, but I will send PATCH v2 later.

I actually did it now: v2 also removes the unneeded casts of calloc() return values.

Quoted 8 lines
Quoted 5 lines

Also, I am of the opinion that it is preferred to use *foo over sizeof(type), i.e. in this case *datum instead of sizeof(class_datum_t). Whilst this _is_ a purely stylistic change, it does carry some practical benifits too. Namely, if the type of datum changes in the future, one can't accidently forget to update the call to calloc()!

I know. I will fix those as well later. Thanks for your comments! So happy having removed 27 useless memset()s!

I changed my mind about this. I simply do not have the nerve to change the sizeof() calls because there is always that slight chance of making a typo or blowing it some other way. I believe the code we are looking at here is very stable at this point. Changing the sizeof() calls would yield only theoretical benefits. So I would prefer playing it safe, but if you feel the need do the fixes, you are welcome to do so.

Now with my PATCH v2, all the changes made are quite mechanical and we can be confident we will not break the policy compiler.

br,
KK

#4 lore raw
Quoted 33 lines · Kalevi Kolttonen
Quoted 32 lines · Kalevi Kolttonen
Quoted 9 lines · Rahul Sandhu

On Thu May 7, 2026 at 10:38 PM BST, Kalevi Kolttonen wrote:

This overall looks like a good change. I have a few nit suggestions.

Quoted 5 lines
---
 checkpolicy/policy_define.c | 81 +++++++++++++------------------------
 1 file changed, 27 insertions(+), 54 deletions(-)
Quoted 15 lines

checkpolicy/policy_define.c int define_class(void) · line 178

Quoted 10 lines
checkpolicy/policy_define.c +1 -1
index b73de45c..a08cdf59 100644
@@ -175,12 +175,11 @@ int define_class(void)
 		yyerror("no class name for class definition?");
 		return -1;
 	}
-	datum = (class_datum_t *) malloc(sizeof(class_datum_t));
+	datum = (class_datum_t *) calloc(1, sizeof(class_datum_t));

What is the point of the cast from void * here? I do realize that this is not something that you introduced, but given that said cast is not consistently applied, even in this file, and is a bit pointless, I'd personally say we may as well just drop it whilst we're at it.

That is *exactly* what I was thinking of, too. I am no C guru but I remember learning from somewhere (maybe the old C FAQ?) that it is in general a bad habit to cast malloc() and calloc() return values because it is at best useless and when erroneous, could mask errors.

Quoted 5 lines
Quoted 1 line

It is too late for today, but I will send PATCH v2 later.

I actually did it now: v2 also removes the unneeded casts of calloc() return values.

o/

Quoted 9 lines
Quoted 8 lines
Quoted 5 lines

Also, I am of the opinion that it is preferred to use *foo over sizeof(type), i.e. in this case *datum instead of sizeof(class_datum_t). Whilst this _is_ a purely stylistic change, it does carry some practical benifits too. Namely, if the type of datum changes in the future, one can't accidently forget to update the call to calloc()!

I know. I will fix those as well later. Thanks for your comments! So happy having removed 27 useless memset()s!

Quoted 7 lines

I changed my mind about this. I simply do not have the nerve to change the sizeof() calls because there is always that slight chance of making a typo or blowing it some other way. I believe the code we are looking at here is very stable at this point. Changing the sizeof() calls would yield only theoretical benefits. So I would prefer playing it safe, but if you feel the need do the fixes, you are welcome to do so.

That's fine. Granted, there's nothing to play that's "unsafe" about preferring *foo over sizeof(foo), but I understand not wanting to make loads of edits.

This may be better off as something done treewide as a separate patch anyway, and if so something to be mentioned in the style guidelines if it is desired such that there is some reference of consistent enforcement.

Quoted 5 lines

Now with my PATCH v2, all the changes made are quite mechanical and we can be confident we will not break the policy compiler.

br,
KK

#5 lore raw
Quoted 2 lines · Rahul Sandhu

That's fine. Granted, there's nothing to play that's "unsafe" about preferring *foo over sizeof(foo), but I understand not wanting to make loads of edits.

Thanks for understanding.

In principle you are right, but I am just afraid of missing something like '*' in some call site and then accidentally allocating too little. The last thing I want to do is to break something, although it will be inevitable in life in general.

Quoted 3 lines

This may be better off as something done treewide as a separate patch anyway, and if so something to be mentioned in the style guidelines if it is desired such that there is some reference of consistent enforcement.

Well, it is a matter of common sense to prefer calloc() in contexts like these since it is only one line of code versus two using malloc()+memset().

So it is probably not worth it to single out this pattern in guidelines.

Please let me know if you are ready to accept further changes to the file policy_define.c. I went through the file and found some miscellaneous minor things to fix there (i.e. unneeded tests that can be safely omitted, most importantly three missing checks for memory allocation failure, some code simplification by preferring strcasecmp() over two strcmp() calls, and one malloc() cast removal).

br,
KK

#6 lore raw
Quoted 11 lines · Kalevi Kolttonen
Quoted 2 lines · Rahul Sandhu

That's fine. Granted, there's nothing to play that's "unsafe" about preferring *foo over sizeof(foo), but I understand not wanting to make loads of edits.

Thanks for understanding.

In principle you are right, but I am just afraid of missing something like '*' in some call site and then accidentally allocating too little. The last thing I want to do is to break something, although it will be inevitable in life in general.

Quoted 6 lines
Quoted 3 lines

This may be better off as something done treewide as a separate patch anyway, and if so something to be mentioned in the style guidelines if it is desired such that there is some reference of consistent enforcement.

Well, it is a matter of common sense to prefer calloc() in contexts like these since it is only one line of code versus two using malloc()+memset().

By this pattern, I was referring to *foo over sizeof(foo), not your patch; sorry for the confusion.

Quoted 10 lines

So it is probably not worth it to single out this pattern in guidelines.

Please let me know if you are ready to accept further changes to the file policy_define.c. I went through the file and found some miscellaneous minor things to fix there (i.e. unneeded tests that can be safely omitted, most importantly three missing checks for memory allocation failure, some code simplification by preferring strcasecmp() over two strcmp() calls, and one malloc() cast removal).

If you're making a number of refactors, you might prefer to send a patch series for them given that they seem to be related.

Quoted 2 lines

br,
KK

#7 lore raw
Quoted 2 lines · Rahul Sandhu

If you're making a number of refactors, you might prefer to send a patch series for them given that they seem to be related.

The common feature of my forthcoming patches is that all of them are minor fixes that are safe to apply without fearing regressions, but I can certainly split the changes to be a series. I just need to base the patches on my previous branch with those calloc() changes and not the main branch.

br,
KK