Replace malloc() + memset() with calloc()
authorHauke Mehrtens <hauke@hauke-m.de>
Sun, 4 Oct 2020 15:14:51 +0000 (17:14 +0200)
committerPetr Štetiar <ynezz@true.cz>
Tue, 6 Oct 2020 06:35:23 +0000 (08:35 +0200)
Instead of manually clearing the memory with memset() use calloc().

Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
cli.c
libuci.c
ucimap.c
util.c

diff --git a/cli.c b/cli.c
index 6ba97ea0742494831edf468f6b16ddd33ea2f479..267437d096d227519220380b698a164b216dc94c 100644 (file)
--- 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;
index 786d03511b063ac80ec8c887f86e55dfd3d8d819..ae4c96476507c42e55f6817dd67d8a5c15bbcf1a 100644 (file)
--- 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);
index c46cf45a40d7a4f222d8353d42f6e45c706f7bc7..758a5eabda266149e08a52c5f85872156e65b91c 100644 (file)
--- 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 8572e8130555c7aff49e8e8b578841b6ef387825..61e42cd3b061c5e17f9950339e815ebbac96b68d 100644 (file)
--- 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;
 }