use calloc instead of malloc+memset
authorFelix Fietkau <nbd@openwrt.org>
Mon, 30 Jun 2014 16:46:08 +0000 (18:46 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Mon, 30 Jun 2014 16:46:08 +0000 (18:46 +0200)
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
forwards.c
includes.c
ipsets.c
main.c
redirects.c
ubus.c
utils.c
zones.c

index c7e7ba1874fb2fe2cc51a8682d4fd8c51027f4e5..e27e4ee4c4dabbc9961dd2a4e5261f3e889871d5 100644 (file)
@@ -48,13 +48,10 @@ fw3_load_forwards(struct fw3_state *state, struct uci_package *p)
                if (strcmp(s->type, "forwarding"))
                        continue;
 
-               forward = malloc(sizeof(*forward));
-
+               forward = calloc(1, sizeof(*forward));
                if (!forward)
                        continue;
 
-               memset(forward, 0, sizeof(*forward));
-
                forward->enabled = true;
 
                fw3_parse_options(forward, fw3_forward_opts, s);
index 5022fbd71882a13b7f41dfb3c52e91110534f73a..e2450a8da7e7c52d13bcff0852524b6696c325ed 100644 (file)
@@ -47,13 +47,10 @@ fw3_load_includes(struct fw3_state *state, struct uci_package *p)
                if (strcmp(s->type, "include"))
                        continue;
 
-               include = malloc(sizeof(*include));
-
+               include = calloc(1, sizeof(*include));
                if (!include)
                        continue;
 
-               memset(include, 0, sizeof(*include));
-
                include->name = e->name;
                include->enabled = true;
 
index 8f88885546c68bb16f124294c51b073d10be68a2..5b319a58e8fbf5120f3dbc08494906aadb79ce30 100644 (file)
--- a/ipsets.c
+++ b/ipsets.c
@@ -203,13 +203,10 @@ fw3_alloc_ipset(void)
 {
        struct fw3_ipset *ipset;
 
-       ipset = malloc(sizeof(*ipset));
-
+       ipset = calloc(1, sizeof(*ipset));
        if (!ipset)
                return NULL;
 
-       memset(ipset, 0, sizeof(*ipset));
-
        INIT_LIST_HEAD(&ipset->datatypes);
 
        ipset->enabled = true;
diff --git a/main.c b/main.c
index 17d71d19ea331c0d26279baa7348ac709289782e..455c049cbb6a5678e286b4d605cd6c4cc810b9ef 100644 (file)
--- a/main.c
+++ b/main.c
@@ -45,12 +45,10 @@ build_state(bool runtime)
        struct uci_package *p = NULL;
        FILE *sf;
 
-       state = malloc(sizeof(*state));
-
+       state = calloc(1, sizeof(*state));
        if (!state)
                error("Out of memory");
 
-       memset(state, 0, sizeof(*state));
        state->uci = uci_alloc_context();
 
        if (!state->uci)
index a21998bafb3bedf32fb1924d08f37ffd24d1d72d..f1fa1f431a79d8bb11daeaec5a7be9854933fc6d 100644 (file)
@@ -232,13 +232,10 @@ fw3_load_redirects(struct fw3_state *state, struct uci_package *p)
                if (strcmp(s->type, "redirect"))
                        continue;
 
-               redir = malloc(sizeof(*redir));
-
+               redir = calloc(1, sizeof(*redir));
                if (!redir)
                        continue;
 
-               memset(redir, 0, sizeof(*redir));
-
                INIT_LIST_HEAD(&redir->proto);
                INIT_LIST_HEAD(&redir->mac_src);
 
diff --git a/ubus.c b/ubus.c
index f5fd8c4b7a217767be7499565c60e4194c08a561..34e21b8d5b1e1bb8cbb659231d0818bbdfa68c58 100644 (file)
--- a/ubus.c
+++ b/ubus.c
@@ -68,13 +68,10 @@ parse_subnet(enum fw3_family family, struct blob_attr *dict, int rem)
        struct blob_attr *cur;
        struct fw3_address *addr;
 
-       addr = malloc(sizeof(*addr));
-
+       addr = calloc(1, sizeof(*addr));
        if (!addr)
                return NULL;
 
-       memset(addr, 0, sizeof(*addr));
-
        addr->set = true;
        addr->family = family;
 
@@ -121,16 +118,14 @@ invoke_common(const char *net, bool device)
                return NULL;
 
        if (device)
-               dev = malloc(sizeof(*dev));
+               dev = calloc(1, sizeof(*dev));
        else
                addr = malloc(sizeof(*addr));
 
        if ((device && !dev) || (!device && !addr))
                goto fail;
 
-       if (device)
-               memset(dev, 0, sizeof(*dev));
-       else
+       if (!device)
                INIT_LIST_HEAD(addr);
 
        blobmsg_for_each_attr(c, interfaces, r) {
diff --git a/utils.c b/utils.c
index 8ed52be4814dff3aa885cb6646b0c10b10846fcc..4f309559088ee27da28fbbcf26b419ec76274948 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -225,7 +225,7 @@ __fw3_command_pipe(bool silent, const char *command, ...)
                return false;
 
        argn = 2;
-       args = malloc(argn * sizeof(arg));
+       args = calloc(argn, sizeof(arg));
 
        if (!args)
                return false;
diff --git a/zones.c b/zones.c
index fe507b0d82b7fc784aeb27ab057a93739e3e0e86..53c62461b927b2ae61dca04fe47335ba32870b33 100644 (file)
--- a/zones.c
+++ b/zones.c
@@ -128,13 +128,10 @@ fw3_alloc_zone(void)
 {
        struct fw3_zone *zone;
 
-       zone = malloc(sizeof(*zone));
-
+       zone = calloc(1, sizeof(*zone));
        if (!zone)
                return NULL;
 
-       memset(zone, 0, sizeof(*zone));
-
        INIT_LIST_HEAD(&zone->networks);
        INIT_LIST_HEAD(&zone->devices);
        INIT_LIST_HEAD(&zone->subnets);
@@ -683,12 +680,10 @@ fw3_resolve_zone_addresses(struct fw3_zone *zone)
        struct fw3_address *addr, *tmp;
        struct list_head *addrs, *all;
 
-       all = malloc(sizeof(*all));
-
+       all = calloc(1, sizeof(*all));
        if (!all)
                return NULL;
 
-       memset(all, 0, sizeof(*all));
        INIT_LIST_HEAD(all);
 
        list_for_each_entry(net, &zone->networks, list)