From 8135d9bc0cf48745e2f5894c7ff068495e350dc3 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Fri, 30 Oct 2020 15:35:11 +0100 Subject: [PATCH] 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. --- src/pattern.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/pattern.c b/src/pattern.c index f1accdcb6e..3d8226fc23 100644 --- a/src/pattern.c +++ b/src/pattern.c @@ -1825,7 +1825,7 @@ struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned { struct pat_ref *ref; - ref = malloc(sizeof(*ref)); + ref = calloc(1, sizeof(*ref)); if (!ref) return NULL; @@ -1836,8 +1836,6 @@ struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned return NULL; } } - else - ref->display = NULL; ref->reference = strdup(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; - ref = malloc(sizeof(*ref)); + ref = calloc(1, sizeof(*ref)); if (!ref) return NULL; @@ -1879,8 +1877,6 @@ struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int f return NULL; } } - else - ref->display = NULL; ref->reference = NULL; 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; - elt = malloc(sizeof(*elt)); + elt = calloc(1, sizeof(*elt)); if (!elt) goto fail; @@ -1916,8 +1912,6 @@ struct pat_ref_elt *pat_ref_append(struct pat_ref *ref, const char *pattern, con if (!elt->sample) goto fail; } - else - elt->sample = NULL; LIST_INIT(&elt->back_refs); 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; /* Memory and initialization of the chain element. */ - list = malloc(sizeof(*list)); + list = calloc(1, sizeof(*list)); if (!list) { memprintf(err, "out of memory"); 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 (!expr) { /* Get a lot of memory for the expr struct. */ - expr = malloc(sizeof(*expr)); + expr = calloc(1, sizeof(*expr)); if (!expr) { free(list); memprintf(err, "out of memory");