]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
CLEANUP: pattern: use calloc() rather than malloc for structures
authorWilly Tarreau <w@1wt.eu>
Fri, 30 Oct 2020 14:35:11 +0000 (15:35 +0100)
committerWilly Tarreau <w@1wt.eu>
Sat, 31 Oct 2020 12:14:10 +0000 (13:14 +0100)
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

index f1accdcb6e43f11ebedc0e935c51bb91dc6abd51..3d8226fc2388b09e104a58a1a8ef5af3b6622e6a 100644 (file)
@@ -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");