[PATCH uci v2 4/4] Replace malloc() + memset() with calloc()

Hauke Mehrtens hauke at hauke-m.de
Sun Oct 4 11:14:51 EDT 2020


Instead of manually clearing the memory with memset() use calloc().

Signed-off-by: Hauke Mehrtens <hauke at hauke-m.de>
---
 cli.c    | 3 +--
 libuci.c | 3 +--
 ucimap.c | 6 ++----
 util.c   | 3 +--
 4 files changed, 5 insertions(+), 10 deletions(-)

diff --git a/cli.c b/cli.c
index 6ba97ea..267437d 100644
--- a/cli.c
+++ b/cli.c
@@ -100,10 +100,9 @@ uci_lookup_section_ref(struct uci_section *s)
 		ti = ti->next;
 	}
 	if (!ti) {
-		ti = malloc(sizeof(struct uci_type_list));
+		ti = calloc(1, sizeof(struct uci_type_list));
 		if (!ti)
 			return NULL;
-		memset(ti, 0, sizeof(struct uci_type_list));
 		ti->next = type_list;
 		type_list = ti;
 		ti->name = s->type;
diff --git a/libuci.c b/libuci.c
index 786d035..ae4c964 100644
--- a/libuci.c
+++ b/libuci.c
@@ -48,11 +48,10 @@ struct uci_context *uci_alloc_context(void)
 {
 	struct uci_context *ctx;
 
-	ctx = (struct uci_context *) malloc(sizeof(struct uci_context));
+	ctx = (struct uci_context *) calloc(1, sizeof(struct uci_context));
 	if (!ctx)
 		return NULL;
 
-	memset(ctx, 0, sizeof(struct uci_context));
 	uci_list_init(&ctx->root);
 	uci_list_init(&ctx->delta_path);
 	uci_list_init(&ctx->backends);
diff --git a/ucimap.c b/ucimap.c
index c46cf45..758a5ea 100644
--- a/ucimap.c
+++ b/ucimap.c
@@ -661,11 +661,10 @@ ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucim
 			size = sizeof(struct ucimap_list) +
 				n_elements * sizeof(union ucimap_data);
 
-			data->list = malloc(size);
+			data->list = calloc(1, size);
 			if (!data->list)
 				goto error_mem;
 
-			memset(data->list, 0, size);
 			data->list->size = n_elements;
 		} else {
 			ucimap_count_alloc(om, &n_alloc, &n_alloc_custom);
@@ -897,10 +896,9 @@ ucimap_parse(struct uci_map *map, struct uci_package *pkg)
 					continue;
 				memset(sd, 0, sizeof(struct ucimap_section_data));
 			} else {
-				sd = malloc(sm->alloc_len);
+				sd = calloc(1, sm->alloc_len);
 				if (!sd)
 					continue;
-				memset(sd, 0, sm->alloc_len);
 				sd = ucimap_ptr_section(sm, sd);
 			}
 
diff --git a/util.c b/util.c
index 8572e81..61e42cd 100644
--- a/util.c
+++ b/util.c
@@ -36,10 +36,9 @@ __private void *uci_malloc(struct uci_context *ctx, size_t size)
 {
 	void *ptr;
 
-	ptr = malloc(size);
+	ptr = calloc(1, size);
 	if (!ptr)
 		UCI_THROW(ctx, UCI_ERR_MEM);
-	memset(ptr, 0, size);
 
 	return ptr;
 }
-- 
2.20.1




More information about the openwrt-devel mailing list