]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
CLEANUP: uniformize last argument of malloc/calloc
authorVincent Bernat <vincent@bernat.im>
Sun, 3 Apr 2016 11:48:43 +0000 (13:48 +0200)
committerWilly Tarreau <w@1wt.eu>
Sun, 3 Apr 2016 12:17:42 +0000 (14:17 +0200)
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
  )

18 files changed:
src/51d.c
src/acl.c
src/cfgparse.c
src/compression.c
src/dns.c
src/dumpstats.c
src/flt_http_comp.c
src/log.c
src/namespace.c
src/peers.c
src/proto_http.c
src/proto_tcp.c
src/proxy.c
src/regex.c
src/sample.c
src/server.c
src/ssl_sock.c
src/uri_auth.c

index f6e497999f80789838553e4756967fcb14286cbf..3aa5b860b2f5689291b1ffed6ebdef1619171b1d 100644 (file)
--- 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;
index 0b88284efe838b14e64baf5547178b3e9be54015..b275a246a3e4606fd97623d910adb5cad49baf3b 100644 (file)
--- 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;
 
index 2dba02aa296cecba5894c98ae8ce0120f8f158f8..abe2fe8660bba3dcf50000a57dc30b5a93608f99 100644 (file)
@@ -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;
index 7b5a93915f9cf10e976d0c7c926b1ec244db7086..8d095855f1937dc197d0a9b282a29a670ae60c21 100644 (file)
@@ -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;
index 14c3b6b61682cd3efd651a59ef43f39f5cb065a5..1ce786dc5bab175a34d89d22fe578b87e3477fa2 100644 (file)
--- 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;
index 7291af690fdbdb28c29e86a3ac12aa9eef869fac..7e2a1d2958a0cf5e4fe4c760651bff7ca25ad1ee 100644 (file)
@@ -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;
 
index 6bbf6a1a14bdf295cc80115150f8bc104e28f6e0..ee8f13bf15a8c0aab558fa1694c58c6fbe260f8a 100644 (file)
@@ -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
index 4d496cdc0dca41959cf0917ab684814d46db4331..47a4317e2e6126c100127c05b7ca5315309ed89c 100644 (file)
--- 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;
index e9262e037fce14116a3cf9e14ba57891bea8fba8..e5ebfd77672fed460c4d80eb5cfd680cce499817 100644 (file)
@@ -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;
index 56256fad1f7c204eb565979a96f80e66c294aef1..bf22b9344292c69ff7cf54c505200be47849a072 100644 (file)
@@ -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)
index dc3fed51c74108b097a55cf709a9b904956cea7f..74cd260c34afe0b078eaa1aae877acf661631190 100644 (file)
@@ -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;
index cce0acbfae2aab9e88c14d668f79166ae16bf0a0..a44912af46549c2450b0edfd955e966e9a7b9414 100644 (file)
@@ -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;
index 5fb5b931710e650984adc6028141a41211da2cf9..b90773fa4eae8f2d6015fe836d44e8a2ed6e4700 100644 (file)
@@ -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;
index c83e48268f0a896e351c24fc9c02e87dc722e507..be4fe5b24596dc0594a8bad4b0a02833fb08b654 100644 (file)
@@ -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;
index fc4261064982ad3ecdda49b1cd4b4ae8274eeb62..8a2fa4f3c6db0671c27fd89d31da863a90f2b5b0 100644 (file)
@@ -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;
 
index d511a2aba22bc6681308798dda6e316313cf97d3..72799bb541b6c97888959e9dd4c5edfa889f2368 100644 (file)
@@ -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);
index 7b09c8e147586bd34f49ebd61a8c0e1553bf9294..8e577abd95acb7dbeecb42cb57db82d6d7301e7f 100644 (file)
@@ -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) {
index c03acbb66370b37b0ec793dced34d2f9286ec0e7..cfe1f4c10d9c5fe8b16c36750c34f47b717021be 100644 (file)
@@ -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;