CLEANUP: pattern: use calloc() rather than malloc for structures

It's particularly difficult to make sure that the various pattern
structures are properly initialized given that they can be allocated
at multiple places and systematically via malloc() instead of calloc(),
thus not even leaving the possibility of default values. Let's adjust
a few of them.
This commit is contained in:
Willy Tarreau 2020-10-30 15:35:11 +01:00
parent 6bedf151e1
commit 8135d9bc0c
1 changed files with 5 additions and 11 deletions

View File

@ -1825,7 +1825,7 @@ struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned
{ {
struct pat_ref *ref; struct pat_ref *ref;
ref = malloc(sizeof(*ref)); ref = calloc(1, sizeof(*ref));
if (!ref) if (!ref)
return NULL; return NULL;
@ -1836,8 +1836,6 @@ struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned
return NULL; return NULL;
} }
} }
else
ref->display = NULL;
ref->reference = strdup(reference); ref->reference = strdup(reference);
if (!ref->reference) { if (!ref->reference) {
@ -1868,7 +1866,7 @@ struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int f
{ {
struct pat_ref *ref; struct pat_ref *ref;
ref = malloc(sizeof(*ref)); ref = calloc(1, sizeof(*ref));
if (!ref) if (!ref)
return NULL; return NULL;
@ -1879,8 +1877,6 @@ struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int f
return NULL; return NULL;
} }
} }
else
ref->display = NULL;
ref->reference = NULL; ref->reference = NULL;
ref->flags = flags; ref->flags = flags;
@ -1901,7 +1897,7 @@ struct pat_ref_elt *pat_ref_append(struct pat_ref *ref, const char *pattern, con
{ {
struct pat_ref_elt *elt; struct pat_ref_elt *elt;
elt = malloc(sizeof(*elt)); elt = calloc(1, sizeof(*elt));
if (!elt) if (!elt)
goto fail; goto fail;
@ -1916,8 +1912,6 @@ struct pat_ref_elt *pat_ref_append(struct pat_ref *ref, const char *pattern, con
if (!elt->sample) if (!elt->sample)
goto fail; goto fail;
} }
else
elt->sample = NULL;
LIST_INIT(&elt->back_refs); LIST_INIT(&elt->back_refs);
LIST_ADDQ(&ref->head, &elt->list); LIST_ADDQ(&ref->head, &elt->list);
@ -2180,7 +2174,7 @@ struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref
*reuse = 0; *reuse = 0;
/* Memory and initialization of the chain element. */ /* Memory and initialization of the chain element. */
list = malloc(sizeof(*list)); list = calloc(1, sizeof(*list));
if (!list) { if (!list) {
memprintf(err, "out of memory"); memprintf(err, "out of memory");
return NULL; return NULL;
@ -2206,7 +2200,7 @@ struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref
/* If no similar expr was found, we create new expr. */ /* If no similar expr was found, we create new expr. */
if (!expr) { if (!expr) {
/* Get a lot of memory for the expr struct. */ /* Get a lot of memory for the expr struct. */
expr = malloc(sizeof(*expr)); expr = calloc(1, sizeof(*expr));
if (!expr) { if (!expr) {
free(list); free(list);
memprintf(err, "out of memory"); memprintf(err, "out of memory");