CLEANUP: uniformize last argument of malloc/calloc
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
)
diff --git a/src/51d.c b/src/51d.c
index f6e4979..3aa5b86 100644
--- a/src/51d.c
+++ b/src/51d.c
@@ -54,7 +54,7 @@
}
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_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 0b88284..b275a24 100644
--- a/src/acl.c
+++ b/src/acl.c
@@ -176,7 +176,7 @@
/* 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 @@
}
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 2dba02a..abe2fe8 100644
--- a/src/cfgparse.c
+++ b/src/cfgparse.c
@@ -290,7 +290,7 @@
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 @@
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 @@
}
}
- 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 @@
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 @@
}
}
- 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 @@
}
}
- 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 @@
}
}
- 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 @@
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 @@
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 @@
/* 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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 7b5a939..8d09585 100644
--- a/src/compression.c
+++ b/src/compression.c
@@ -109,7 +109,7 @@
{
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 @@
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 14c3b6b..1ce786d 100644
--- a/src/dns.c
+++ b/src/dns.c
@@ -909,7 +909,7 @@
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 7291af6..7e2a1d2 100644
--- a/src/dumpstats.c
+++ b/src/dumpstats.c
@@ -413,7 +413,7 @@
{
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 6bbf6a1..ee8f13b 100644
--- a/src/flt_http_comp.c
+++ b/src/flt_http_comp.c
@@ -772,7 +772,7 @@
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 4d496cd..47a4317 100644
--- a/src/log.c
+++ b/src/log.c
@@ -339,7 +339,7 @@
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 @@
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 @@
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 e9262e0..e5ebfd7 100644
--- a/src/namespace.c
+++ b/src/namespace.c
@@ -67,7 +67,7 @@
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 56256fa..bf22b93 100644
--- a/src/peers.c
+++ b/src/peers.c
@@ -1975,7 +1975,7 @@
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 dc3fed5..74cd260 100644
--- a/src/proto_http.c
+++ b/src/proto_http.c
@@ -8785,7 +8785,7 @@
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 @@
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 cce0acb..a44912a 100644
--- a/src/proto_tcp.c
+++ b/src/proto_tcp.c
@@ -1641,7 +1641,7 @@
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 5fb5b93..b90773f 100644
--- a/src/proxy.c
+++ b/src/proxy.c
@@ -415,7 +415,7 @@
}
/* 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 c83e482..be4fe5b 100644
--- a/src/regex.c
+++ b/src/regex.c
@@ -139,7 +139,7 @@
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 fc42610..8a2fa4f 100644
--- a/src/sample.c
+++ b/src/sample.c
@@ -855,7 +855,7 @@
}
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 @@
}
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 d511a2a..72799bb 100644
--- a/src/server.c
+++ b/src/server.c
@@ -874,7 +874,7 @@
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 @@
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 7b09c8e..8e577ab 100644
--- a/src/ssl_sock.c
+++ b/src/ssl_sock.c
@@ -742,7 +742,7 @@
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 @@
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 @@
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 @@
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 c03acbb..cfe1f4c 100644
--- a/src/uri_auth.c
+++ b/src/uri_auth.c
@@ -242,7 +242,7 @@
return u;
}
- newuser = calloc(1, sizeof(struct auth_users));
+ newuser = calloc(1, sizeof(*newuser));
if (!newuser)
return NULL;