From: Vincent Bernat <[email protected]>

Instead of repeating the type of the LHS argument (sizeof(struct ...))
in calls to malloc/calloc, we directly use the pointer
name (sizeof(*...)). The following Coccinelle patch was used:

@@
type T;
T *x;
@@

  x = malloc(
- sizeof(T)
+ sizeof(*x)
  )

@@
type T;
T *x;
@@

  x = calloc(1,
- sizeof(T)
+ sizeof(*x)
  )

When the LHS is not just a variable name, no change is made. Moreover,
the following patch was used to ensure that "1" is consistently used as
a first argument of calloc, not the last one:

@@
@@

  calloc(
+ 1,
  ...
- ,1
  )
---
 src/51d.c           |  4 ++--
 src/acl.c           |  4 ++--
 src/cfgparse.c      | 32 ++++++++++++++++----------------
 src/compression.c   |  4 ++--
 src/dns.c           |  2 +-
 src/dumpstats.c     |  2 +-
 src/flt_http_comp.c |  2 +-
 src/log.c           | 10 +++++-----
 src/namespace.c     |  2 +-
 src/peers.c         |  2 +-
 src/proto_http.c    |  4 ++--
 src/proto_tcp.c     |  2 +-
 src/proxy.c         |  2 +-
 src/regex.c         |  2 +-
 src/sample.c        |  4 ++--
 src/server.c        |  4 ++--
 src/ssl_sock.c      |  8 ++++----
 src/uri_auth.c      |  2 +-
 18 files changed, 46 insertions(+), 46 deletions(-)

diff --git a/src/51d.c b/src/51d.c
index f6e497999f80..3aa5b860b2f5 100644
--- a/src/51d.c
+++ b/src/51d.c
@@ -54,7 +54,7 @@ static int _51d_property_name_list(char **args, int 
section_type, struct proxy *
        }
 
        while (*(args[cur_arg])) {
-               name = calloc(1, sizeof(struct _51d_property_names));
+               name = calloc(1, sizeof(*name));
                name->name = strdup(args[cur_arg]);
                LIST_ADDQ(&global._51degrees.property_names, &name->list);
                ++cur_arg;
@@ -158,7 +158,7 @@ static void *_51d_malloc(int size)
  */
 static void _51d_insert_cache_entry(struct sample *smp, struct lru64 *lru, 
void* domain)
 {
-       struct chunk *cache_entry = _51d_malloc(sizeof(struct chunk));
+       struct chunk *cache_entry = _51d_malloc(sizeof(*cache_entry));
 
        if (!cache_entry)
                return;
diff --git a/src/acl.c b/src/acl.c
index 0b88284efe83..b275a246a3e4 100644
--- a/src/acl.c
+++ b/src/acl.c
@@ -176,7 +176,7 @@ struct acl_expr *parse_acl_expr(const char **args, char 
**err, struct arg_list *
                /* OK we have a real ACL keyword */
 
                /* build new sample expression for this ACL */
-               smp = calloc(1, sizeof(struct sample_expr));
+               smp = calloc(1, sizeof(*smp));
                if (!smp) {
                        memprintf(err, "out of memory when parsing ACL 
expression");
                        goto out_return;
@@ -298,7 +298,7 @@ struct acl_expr *parse_acl_expr(const char **args, char 
**err, struct arg_list *
                        }
 
                        cur_type = conv->out_type;
-                       conv_expr = calloc(1, sizeof(struct sample_conv_expr));
+                       conv_expr = calloc(1, sizeof(*conv_expr));
                        if (!conv_expr)
                                goto out_free_smp;
 
diff --git a/src/cfgparse.c b/src/cfgparse.c
index 2dba02aa296c..abe2fe8660bb 100644
--- a/src/cfgparse.c
+++ b/src/cfgparse.c
@@ -290,7 +290,7 @@ int str2listener(char *str, struct proxy *curproxy, struct 
bind_conf *bind_conf,
                ss = *ss2;
 
                for (; port <= end; port++) {
-                       l = calloc(1, sizeof(struct listener));
+                       l = calloc(1, sizeof(*l));
                        l->obj_type = OBJ_TYPE_LISTENER;
                        LIST_ADDQ(&curproxy->conf.listeners, &l->by_fe);
                        LIST_ADDQ(&bind_conf->listeners, &l->by_bind);
@@ -1571,7 +1571,7 @@ int cfg_parse_global(const char *file, int linenum, char 
**args, int kwm)
                        goto out;
                }
 
-               logsrv = calloc(1, sizeof(struct logsrv));
+               logsrv = calloc(1, sizeof(*logsrv));
 
                /* just after the address, a length may be specified */
                if (strcmp(args[arg+2], "len") == 0) {
@@ -2119,7 +2119,7 @@ int cfg_parse_peers(const char *file, int linenum, char 
**args, int kwm)
                        }
                }
 
-               if ((curpeers = calloc(1, sizeof(struct peers))) == NULL) {
+               if ((curpeers = calloc(1, sizeof(*curpeers))) == NULL) {
                        Alert("parsing [%s:%d] : out of memory.\n", file, 
linenum);
                        err_code |= ERR_ALERT | ERR_ABORT;
                        goto out;
@@ -2153,7 +2153,7 @@ int cfg_parse_peers(const char *file, int linenum, char 
**args, int kwm)
                        goto out;
                }
 
-               if ((newpeer = calloc(1, sizeof(struct peer))) == NULL) {
+               if ((newpeer = calloc(1, sizeof(*newpeer))) == NULL) {
                        Alert("parsing [%s:%d] : out of memory.\n", file, 
linenum);
                        err_code |= ERR_ALERT | ERR_ABORT;
                        goto out;
@@ -2316,7 +2316,7 @@ int cfg_parse_resolvers(const char *file, int linenum, 
char **args, int kwm)
                        }
                }
 
-               if ((curr_resolvers = calloc(1, sizeof(struct dns_resolvers))) 
== NULL) {
+               if ((curr_resolvers = calloc(1, sizeof(*curr_resolvers))) == 
NULL) {
                        Alert("parsing [%s:%d] : out of memory.\n", file, 
linenum);
                        err_code |= ERR_ALERT | ERR_ABORT;
                        goto out;
@@ -2364,7 +2364,7 @@ int cfg_parse_resolvers(const char *file, int linenum, 
char **args, int kwm)
                        }
                }
 
-               if ((newnameserver = calloc(1, sizeof(struct dns_nameserver))) 
== NULL) {
+               if ((newnameserver = calloc(1, sizeof(*newnameserver))) == 
NULL) {
                        Alert("parsing [%s:%d] : out of memory.\n", file, 
linenum);
                        err_code |= ERR_ALERT | ERR_ABORT;
                        goto out;
@@ -2535,7 +2535,7 @@ int cfg_parse_mailers(const char *file, int linenum, char 
**args, int kwm)
                        }
                }
 
-               if ((curmailers = calloc(1, sizeof(struct mailers))) == NULL) {
+               if ((curmailers = calloc(1, sizeof(*curmailers))) == NULL) {
                        Alert("parsing [%s:%d] : out of memory.\n", file, 
linenum);
                        err_code |= ERR_ALERT | ERR_ABORT;
                        goto out;
@@ -2570,7 +2570,7 @@ int cfg_parse_mailers(const char *file, int linenum, char 
**args, int kwm)
                        goto out;
                }
 
-               if ((newmailer = calloc(1, sizeof(struct mailer))) == NULL) {
+               if ((newmailer = calloc(1, sizeof(*newmailer))) == NULL) {
                        Alert("parsing [%s:%d] : out of memory.\n", file, 
linenum);
                        err_code |= ERR_ALERT | ERR_ABORT;
                        goto out;
@@ -2725,7 +2725,7 @@ int cfg_parse_listen(const char *file, int linenum, char 
**args, int kwm)
                                err_code |= ERR_ALERT | ERR_FATAL;
                }
 
-               if ((curproxy = calloc(1, sizeof(struct proxy))) == NULL) {
+               if ((curproxy = calloc(1, sizeof(*curproxy))) == NULL) {
                        Alert("parsing [%s:%d] : out of memory.\n", file, 
linenum);
                        err_code |= ERR_ALERT | ERR_ABORT;
                        goto out;
@@ -2911,7 +2911,7 @@ int cfg_parse_listen(const char *file, int linenum, char 
**args, int kwm)
 
                /* copy default logsrvs to curproxy */
                list_for_each_entry(tmplogsrv, &defproxy.logsrvs, list) {
-                       struct logsrv *node = malloc(sizeof(struct logsrv));
+                       struct logsrv *node = malloc(sizeof(*node));
                        memcpy(node, tmplogsrv, sizeof(struct logsrv));
                        LIST_INIT(&node->list);
                        LIST_ADDQ(&curproxy->logsrvs, &node->list);
@@ -3739,7 +3739,7 @@ int cfg_parse_listen(const char *file, int linenum, char 
**args, int kwm)
                                goto out;
                        }
 
-                       hdr = calloc(sizeof(struct cap_hdr), 1);
+                       hdr = calloc(1, sizeof(*hdr));
                        hdr->next = curproxy->req_cap;
                        hdr->name = strdup(args[3]);
                        hdr->namelen = strlen(args[3]);
@@ -3767,7 +3767,7 @@ int cfg_parse_listen(const char *file, int linenum, char 
**args, int kwm)
                                err_code |= ERR_ALERT | ERR_FATAL;
                                goto out;
                        }
-                       hdr = calloc(sizeof(struct cap_hdr), 1);
+                       hdr = calloc(1, sizeof(*hdr));
                        hdr->next = curproxy->rsp_cap;
                        hdr->name = strdup(args[3]);
                        hdr->namelen = strlen(args[3]);
@@ -6043,7 +6043,7 @@ stats_error_parsing:
                if (*(args[1]) && *(args[2]) == 0 && !strcmp(args[1], 
"global")) {
                        /* copy global.logrsvs linked list to the end of 
curproxy->logsrvs */
                        list_for_each_entry(tmplogsrv, &global.logsrvs, list) {
-                               struct logsrv *node = malloc(sizeof(struct 
logsrv));
+                               struct logsrv *node = malloc(sizeof(*node));
                                memcpy(node, tmplogsrv, sizeof(struct logsrv));
                                LIST_INIT(&node->list);
                                LIST_ADDQ(&curproxy->logsrvs, &node->list);
@@ -6055,7 +6055,7 @@ stats_error_parsing:
                        int arg = 0;
                        int len = 0;
 
-                       logsrv = calloc(1, sizeof(struct logsrv));
+                       logsrv = calloc(1, sizeof(*logsrv));
 
                        /* just after the address, a length may be specified */
                        if (strcmp(args[arg+2], "len") == 0) {
@@ -6782,7 +6782,7 @@ cfg_parse_users(const char *file, int linenum, char 
**args, int kwm)
                                goto out;
                        }
 
-               newul = calloc(1, sizeof(struct userlist));
+               newul = calloc(1, sizeof(*newul));
                if (!newul) {
                        Alert("parsing [%s:%d]: out of memory.\n", file, 
linenum);
                        err_code |= ERR_ALERT | ERR_ABORT;
@@ -6883,7 +6883,7 @@ cfg_parse_users(const char *file, int linenum, char 
**args, int kwm)
                                goto out;
                        }
 
-               newuser = calloc(1, sizeof(struct auth_users));
+               newuser = calloc(1, sizeof(*newuser));
                if (!newuser) {
                        Alert("parsing [%s:%d]: out of memory.\n", file, 
linenum);
                        err_code |= ERR_ALERT | ERR_ABORT;
diff --git a/src/compression.c b/src/compression.c
index 7b5a93915f9c..8d095855f193 100644
--- a/src/compression.c
+++ b/src/compression.c
@@ -109,7 +109,7 @@ int comp_append_type(struct comp *comp, const char *type)
 {
        struct comp_type *comp_type;
 
-       comp_type = calloc(1, sizeof(struct comp_type));
+       comp_type = calloc(1, sizeof(*comp_type));
        comp_type->name_len = strlen(type);
        comp_type->name = strdup(type);
        comp_type->next = comp->types;
@@ -127,7 +127,7 @@ int comp_append_algo(struct comp *comp, const char *algo)
 
        for (i = 0; comp_algos[i].cfg_name; i++) {
                if (!strcmp(algo, comp_algos[i].cfg_name)) {
-                       comp_algo = calloc(1, sizeof(struct comp_algo));
+                       comp_algo = calloc(1, sizeof(*comp_algo));
                        memmove(comp_algo, &comp_algos[i], sizeof(struct 
comp_algo));
                        comp_algo->next = comp->algos;
                        comp->algos = comp_algo;
diff --git a/src/dns.c b/src/dns.c
index 14c3b6b61682..1ce786dc5bab 100644
--- a/src/dns.c
+++ b/src/dns.c
@@ -909,7 +909,7 @@ int dns_init_resolvers(void)
                curr_resolvers->t = t;
 
                list_for_each_entry(curnameserver, 
&curr_resolvers->nameserver_list, list) {
-                       if ((dgram = calloc(1, sizeof(struct dgram_conn))) == 
NULL) {
+                       if ((dgram = calloc(1, sizeof(*dgram))) == NULL) {
                                Alert("Starting [%s/%s] nameserver: out of 
memory.\n", curr_resolvers->id,
                                                curnameserver->id);
                                return 0;
diff --git a/src/dumpstats.c b/src/dumpstats.c
index 7291af690fdb..7e2a1d2958a0 100644
--- a/src/dumpstats.c
+++ b/src/dumpstats.c
@@ -413,7 +413,7 @@ static struct proxy *alloc_stats_fe(const char *name, const 
char *file, int line
 {
        struct proxy *fe;
 
-       fe = calloc(1, sizeof(struct proxy));
+       fe = calloc(1, sizeof(*fe));
        if (!fe)
                return NULL;
 
diff --git a/src/flt_http_comp.c b/src/flt_http_comp.c
index 6bbf6a1a14bd..ee8f13bf15a8 100644
--- a/src/flt_http_comp.c
+++ b/src/flt_http_comp.c
@@ -772,7 +772,7 @@ parse_compression_options(char **args, int section, struct 
proxy *proxy,
        struct comp    *comp;
 
        if (proxy->comp == NULL) {
-               comp = calloc(1, sizeof(struct comp));
+               comp = calloc(1, sizeof(*comp));
                proxy->comp = comp;
        }
        else
diff --git a/src/log.c b/src/log.c
index 4d496cdc0dca..47a4317e2e61 100644
--- a/src/log.c
+++ b/src/log.c
@@ -339,7 +339,7 @@ int parse_logformat_var(char *arg, int arg_len, char *var, 
int var_len, struct p
                if (strlen(logformat_keywords[j].name) == var_len &&
                    strncmp(var, logformat_keywords[j].name, var_len) == 0) {
                        if (logformat_keywords[j].mode != PR_MODE_HTTP || 
curproxy->mode == PR_MODE_HTTP) {
-                               node = calloc(1, sizeof(struct logformat_node));
+                               node = calloc(1, sizeof(*node));
                                node->type = logformat_keywords[j].type;
                                node->options = *defoptions;
                                if (arg_len) {
@@ -396,15 +396,15 @@ void add_to_logformat_list(char *start, char *end, int 
type, struct list *list_f
        char *str;
 
        if (type == LF_TEXT) { /* type text */
-               struct logformat_node *node = calloc(1, sizeof(struct 
logformat_node));
-               str = calloc(end - start + 1, 1);
+               struct logformat_node *node = calloc(1, sizeof(*node));
+               str = calloc(1, end - start + 1);
                strncpy(str, start, end - start);
                str[end - start] = '\0';
                node->arg = str;
                node->type = LOG_FMT_TEXT; // type string
                LIST_ADDQ(list_format, &node->list);
        } else if (type == LF_SEPARATOR) {
-               struct logformat_node *node = calloc(1, sizeof(struct 
logformat_node));
+               struct logformat_node *node = calloc(1, sizeof(*node));
                node->type = LOG_FMT_SEPARATOR;
                LIST_ADDQ(list_format, &node->list);
        }
@@ -435,7 +435,7 @@ void add_sample_to_logformat_list(char *text, char *arg, 
int arg_len, struct pro
                return;
        }
 
-       node = calloc(1, sizeof(struct logformat_node));
+       node = calloc(1, sizeof(*node));
        node->type = LOG_FMT_EXPR;
        node->expr = expr;
        node->options = options;
diff --git a/src/namespace.c b/src/namespace.c
index e9262e037fce..e5ebfd77672f 100644
--- a/src/namespace.c
+++ b/src/namespace.c
@@ -67,7 +67,7 @@ struct netns_entry* netns_store_insert(const char *ns_name)
        if (fd == -1)
                goto out;
 
-       entry = calloc(1, sizeof(struct netns_entry));
+       entry = calloc(1, sizeof(*entry));
        if (!entry)
                goto out;
        entry->fd = fd;
diff --git a/src/peers.c b/src/peers.c
index 56256fad1f7c..bf22b9344292 100644
--- a/src/peers.c
+++ b/src/peers.c
@@ -1975,7 +1975,7 @@ void peers_register_table(struct peers *peers, struct 
stktable *table)
        int id = 0;
 
        for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
-               st = calloc(1,sizeof(struct shared_table));
+               st = calloc(1,sizeof(*st));
                st->table = table;
                st->next = curpeer->tables;
                if (curpeer->tables)
diff --git a/src/proto_http.c b/src/proto_http.c
index dc3fed51c741..74cd260c34af 100644
--- a/src/proto_http.c
+++ b/src/proto_http.c
@@ -8785,7 +8785,7 @@ struct act_rule *parse_http_req_cond(const char **args, 
const char *file, int li
        int cur_arg;
        char *error;
 
-       rule = calloc(1, sizeof(struct act_rule));
+       rule = calloc(1, sizeof(*rule));
        if (!rule) {
                Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
                goto out_err;
@@ -12421,7 +12421,7 @@ enum act_parse_ret parse_http_req_capture(const char 
**args, int *orig_arg, stru
                        return ACT_RET_PRS_ERR;
                }
 
-               hdr = calloc(sizeof(struct cap_hdr), 1);
+               hdr = calloc(1, sizeof(*hdr));
                hdr->next = px->req_cap;
                hdr->name = NULL; /* not a header capture */
                hdr->namelen = 0;
diff --git a/src/proto_tcp.c b/src/proto_tcp.c
index cce0acbfae2a..a44912af4654 100644
--- a/src/proto_tcp.c
+++ b/src/proto_tcp.c
@@ -1641,7 +1641,7 @@ static int tcp_parse_request_rule(char **args, int arg, 
int section_type,
                        return -1;
                }
 
-               hdr = calloc(sizeof(struct cap_hdr), 1);
+               hdr = calloc(1, sizeof(*hdr));
                hdr->next = curpx->req_cap;
                hdr->name = NULL; /* not a header capture */
                hdr->namelen = 0;
diff --git a/src/proxy.c b/src/proxy.c
index 5fb5b931710e..b90773fa4eae 100644
--- a/src/proxy.c
+++ b/src/proxy.c
@@ -415,7 +415,7 @@ static int proxy_parse_declare(char **args, int section, 
struct proxy *curpx,
                }
 
                /* register the capture. */
-               hdr = calloc(1, sizeof(struct cap_hdr));
+               hdr = calloc(1, sizeof(*hdr));
                hdr->name = NULL; /* not a header capture */
                hdr->namelen = 0;
                hdr->len = len;
diff --git a/src/regex.c b/src/regex.c
index c83e48268f0a..be4fe5b24596 100644
--- a/src/regex.c
+++ b/src/regex.c
@@ -139,7 +139,7 @@ const char *chain_regex(struct hdr_exp **head, struct 
my_regex *preg,
        while (*head != NULL)
                head = &(*head)->next;
 
-       exp = calloc(1, sizeof(struct hdr_exp));
+       exp = calloc(1, sizeof(*exp));
 
        exp->preg = preg;
        exp->replace = replace;
diff --git a/src/sample.c b/src/sample.c
index fc4261064982..8a2fa4f3c6db 100644
--- a/src/sample.c
+++ b/src/sample.c
@@ -855,7 +855,7 @@ struct sample_expr *sample_parse_expr(char **str, int *idx, 
const char *file, in
        }
        prev_type = fetch->out_type;
 
-       expr = calloc(1, sizeof(struct sample_expr));
+       expr = calloc(1, sizeof(*expr));
        if (!expr)
                goto out_error;
 
@@ -958,7 +958,7 @@ struct sample_expr *sample_parse_expr(char **str, int *idx, 
const char *file, in
                }
 
                prev_type = conv->out_type;
-               conv_expr = calloc(1, sizeof(struct sample_conv_expr));
+               conv_expr = calloc(1, sizeof(*conv_expr));
                if (!conv_expr)
                        goto out_error;
 
diff --git a/src/server.c b/src/server.c
index d511a2aba22b..72799bb541b6 100644
--- a/src/server.c
+++ b/src/server.c
@@ -874,7 +874,7 @@ int parse_server(const char *file, int linenum, char 
**args, struct proxy *curpr
                        struct protocol *proto;
                        struct dns_resolution *curr_resolution;
 
-                       if ((newsrv = calloc(1, sizeof(struct server))) == 
NULL) {
+                       if ((newsrv = calloc(1, sizeof(*newsrv))) == NULL) {
                                Alert("parsing [%s:%d] : out of memory.\n", 
file, linenum);
                                err_code |= ERR_ALERT | ERR_ABORT;
                                goto out;
@@ -945,7 +945,7 @@ int parse_server(const char *file, int linenum, char 
**args, struct proxy *curpr
                                goto skip_name_resolution;
 
                        fqdn = NULL;
-                       if ((curr_resolution = calloc(1, sizeof(struct 
dns_resolution))) == NULL)
+                       if ((curr_resolution = calloc(1, 
sizeof(*curr_resolution))) == NULL)
                                goto skip_name_resolution;
 
                        curr_resolution->hostname_dn_len = 
dns_str_to_dn_label_len(newsrv->hostname);
diff --git a/src/ssl_sock.c b/src/ssl_sock.c
index 7b09c8e14758..8e577abd95ac 100644
--- a/src/ssl_sock.c
+++ b/src/ssl_sock.c
@@ -742,7 +742,7 @@ static int ssl_sock_load_ocsp(SSL_CTX *ctx, const char 
*cert_path)
        if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH))
                goto out;
 
-       ocsp = calloc(1, sizeof(struct certificate_ocsp));
+       ocsp = calloc(1, sizeof(*ocsp));
        if (!ocsp)
                goto out;
 
@@ -754,7 +754,7 @@ static int ssl_sock_load_ocsp(SSL_CTX *ctx, const char 
*cert_path)
                ocsp = NULL;
 
        if (!ctx->tlsext_status_cb) {
-               struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(struct 
ocsp_cbk_arg));
+               struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg));
 
                cb_arg->is_single = 1;
                cb_arg->s_ocsp = iocsp;
@@ -897,7 +897,7 @@ static int ssl_sock_load_sctl_from_file(const char 
*sctl_path, struct chunk **sc
        if (ret)
                goto end;
 
-       *sctl = calloc(1, sizeof(struct chunk));
+       *sctl = calloc(1, sizeof(**sctl));
        if (!chunk_dup(*sctl, &trash)) {
                free(*sctl);
                *sctl = NULL;
@@ -5369,7 +5369,7 @@ static int bind_parse_tls_ticket_keys(char **args, int 
cur_arg, struct proxy *px
                return 0;
        }
 
-       keys_ref = malloc(sizeof(struct tls_keys_ref));
+       keys_ref = malloc(sizeof(*keys_ref));
        keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(struct 
tls_sess_key));
 
        if ((f = fopen(args[cur_arg + 1], "r")) == NULL) {
diff --git a/src/uri_auth.c b/src/uri_auth.c
index c03acbb66370..cfe1f4c10d9c 100644
--- a/src/uri_auth.c
+++ b/src/uri_auth.c
@@ -242,7 +242,7 @@ struct uri_auth *stats_add_auth(struct uri_auth **root, 
char *user)
                        return u;
                }
 
-       newuser = calloc(1, sizeof(struct auth_users));
+       newuser = calloc(1, sizeof(*newuser));
        if (!newuser)
                return NULL;
 
-- 
2.8.0.rc3


Reply via email to