Skip to contentSkip to conversation

checkpolicy: use calloc() so no need to do memset()

PATCH Superseded Kalevi Kolttonen 2 participants 3 messages May 1 – May 13 SELinux security module

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 checkpolicy: use calloc() so no need to do memset()

---
 checkpolicy/checkpolicy.c     | 4 +---
 checkpolicy/module_compiler.c | 3 +--
 2 files changed, 2 insertions(+), 5 deletions(-)
checkpolicy/checkpolicy.c +1 -3
index b808e4d0..34c21199 100644
@@ -235,13 +235,11 @@ static int identify_equiv_types(void)
 	 * Create a list of access vector rules for each type
 	 * from the access vector table.
 	 */
-	type_rules = malloc(sizeof(struct avtab_node) * policydb.p_types.nprim);
+	type_rules = calloc(1, sizeof(struct avtab_node) * policydb.p_types.nprim);
 	if (!type_rules) {
 		fprintf(stderr, "out of memory\n");
 		exit(1);
 	}
-	memset(type_rules, 0,
-	       sizeof(struct avtab_node) * policydb.p_types.nprim);
 	if (avtab_map(&policydb.te_avtab, create_type_rules, type_rules))
 		exit(1);
 
checkpolicy/module_compiler.c +1 -2
index 4b2f98d8..a6ecc939 100644
@@ -842,12 +842,11 @@ int require_class(int pass)
 				return -1;
 			}
 			allocated = 1;
-			if ((perm = malloc(sizeof(*perm))) == NULL) {
+			if ((perm = calloc(1, sizeof(*perm))) == NULL) {
 				yyerror("Out of memory!");
 				free(perm_id);
 				return -1;
 			}
-			memset(perm, 0, sizeof(*perm));
 			ret =
 			    hashtab_insert(datum->permissions.table, perm_id,
 					   perm);
Signature · 1 line
2.54.0
#1 lore raw
Quoted 12 lines · Kalevi Kolttonen
---
 checkpolicy/checkpolicy.c     | 4 +---
 checkpolicy/module_compiler.c | 3 +--
 2 files changed, 2 insertions(+), 5 deletions(-)
checkpolicy/checkpolicy.c +0 -0
index b808e4d0..34c21199 100644

checkpolicy/checkpolicy.c static int identify_equiv_types(void) · line 238

Quoted 5 lines
checkpolicy/checkpolicy.c +1 -1
@@ -235,13 +235,11 @@ static int identify_equiv_types(void)
         * Create a list of access vector rules for each type
         * from the access vector table.
         */
-       type_rules = malloc(sizeof(struct avtab_node) * policydb.p_types.nprim);
+       type_rules = calloc(1, sizeof(struct avtab_node) * policydb.p_types.nprim);

p_types.nprim is the number of types, so that is the value that should be used as the number of elements rather than 1.
Thanks,
Jim

Quoted 31 lines
checkpolicy/checkpolicy.c +0 -2
        if (!type_rules) {
                fprintf(stderr, "out of memory\n");
                exit(1);
        }
-       memset(type_rules, 0,
-              sizeof(struct avtab_node) * policydb.p_types.nprim);
        if (avtab_map(&policydb.te_avtab, create_type_rules, type_rules))
                exit(1);
 
checkpolicy/module_compiler.c +1 -3
index 4b2f98d8..a6ecc939 100644
@@ -842,12 +842,11 @@ int require_class(int pass)
                                return -1;
                        }
                        allocated = 1;
-                       if ((perm = malloc(sizeof(*perm))) == NULL) {
+                       if ((perm = calloc(1, sizeof(*perm))) == NULL) {
                                yyerror("Out of memory!");
                                free(perm_id);
                                return -1;
                        }
-                       memset(perm, 0, sizeof(*perm));
                        ret =
                            hashtab_insert(datum->permissions.table, perm_id,
                                           perm);
--

2.54.0

#2 lore raw
Quoted 2 lines · James Carter

p_types.nprim is the number of types, so that is the value that should be used as the number of elements rather than 1.

Yes, thanks, it is cleaner that way. I will send fixed v3.

br,
KK