From 969e212c663dbbfae4b432413f8363072c0834a5 Mon Sep 17 00:00:00 2001 From: Aurelien DARRAGON Date: Wed, 5 Jul 2023 15:52:19 +0200 Subject: [PATCH] MINOR: log: add dup_logsrv() helper function ease code maintenance by introducing dup_logsrv() helper function to properly duplicate an existing logsrv struct. --- include/haproxy/log.h | 1 + src/http_client.c | 7 +----- src/log.c | 51 ++++++++++++++++++++++++++++++++++++++----- src/proxy.c | 8 +------ 4 files changed, 48 insertions(+), 19 deletions(-) diff --git a/include/haproxy/log.h b/include/haproxy/log.h index 1715a0c9d2..1a4c79071a 100644 --- a/include/haproxy/log.h +++ b/include/haproxy/log.h @@ -87,6 +87,7 @@ int add_to_logformat_list(char *start, char *end, int type, struct list *list_fo */ int parse_logformat_string(const char *str, struct proxy *curproxy, struct list *list_format, int options, int cap, char **err); +struct logsrv *dup_logsrv(struct logsrv *def); void free_logsrv(struct logsrv *logsrv); /* Parse "log" keyword and update the linked list. */ diff --git a/src/http_client.c b/src/http_client.c index 8346f02f6d..bebdd7f51e 100644 --- a/src/http_client.c +++ b/src/http_client.c @@ -1369,19 +1369,14 @@ static int httpclient_postcheck_proxy(struct proxy *curproxy) /* copy logs from "global" log list */ list_for_each_entry(logsrv, &global.logsrvs, list) { - struct logsrv *node = malloc(sizeof(*node)); + struct logsrv *node = dup_logsrv(logsrv); if (!node) { memprintf(&errmsg, "out of memory."); err_code |= ERR_ALERT | ERR_FATAL; goto err; } - - memcpy(node, logsrv, sizeof(*node)); - LIST_INIT(&node->list); LIST_APPEND(&curproxy->logsrvs, &node->list); - node->ring_name = logsrv->ring_name ? strdup(logsrv->ring_name) : NULL; - node->conf.file = logsrv->conf.file ? strdup(logsrv->conf.file) : NULL; } if (curproxy->conf.logformat_string) { curproxy->conf.args.ctx = ARGC_LOG; diff --git a/src/log.c b/src/log.c index e8430a88b1..97b809e207 100644 --- a/src/log.c +++ b/src/log.c @@ -734,6 +734,43 @@ int smp_log_range_cmp(const void *a, const void *b) return 0; } +/* tries to duplicate logsrv + * + * Returns the newly allocated and duplicated logsrv or NULL + * in case of error. + */ +struct logsrv *dup_logsrv(struct logsrv *def) +{ + struct logsrv *cpy = malloc(sizeof(*cpy)); + + /* copy everything that can be easily copied */ + memcpy(cpy, def, sizeof(*cpy)); + + /* default values */ + cpy->ring_name = NULL; + cpy->conf.file = NULL; + LIST_INIT(&cpy->list); + HA_SPIN_INIT(&cpy->lock); + + /* special members */ + if (def->ring_name) { + cpy->ring_name = strdup(def->ring_name); + if (!cpy->ring_name) + goto error; + } + if (def->conf.file) { + cpy->conf.file = strdup(def->conf.file); + if (!cpy->conf.file) + goto error; + } + cpy->ref = def; + return cpy; + + error: + free_logsrv(cpy); + return NULL; +} + /* frees log server after freeing all of its allocated fields. The * server must not belong to a list anymore. Logsrv may be NULL, which is * silently ignored. @@ -808,19 +845,21 @@ int parse_logsrv(char **args, struct list *logsrvs, int do_del, const char *file goto skip_logsrv; } - node = malloc(sizeof(*node)); + /* duplicate logsrv from global */ + node = dup_logsrv(logsrv); if (!node) { memprintf(err, "out of memory error"); goto error; } - memcpy(node, logsrv, sizeof(struct logsrv)); - node->ref = logsrv; - LIST_INIT(&node->list); - LIST_APPEND(logsrvs, &node->list); - node->ring_name = logsrv->ring_name ? strdup(logsrv->ring_name) : NULL; + + /* manually override some values */ + ha_free(&node->conf.file); node->conf.file = strdup(file); node->conf.line = linenum; + /* add to list */ + LIST_APPEND(logsrvs, &node->list); + skip_logsrv: continue; } diff --git a/src/proxy.c b/src/proxy.c index ae8049dbb6..714e70b5ae 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -1814,19 +1814,13 @@ static int proxy_defproxy_cpy(struct proxy *curproxy, const struct proxy *defpro /* copy default logsrvs to curproxy */ list_for_each_entry(tmplogsrv, &defproxy->logsrvs, list) { - struct logsrv *node = malloc(sizeof(*node)); + struct logsrv *node = dup_logsrv(tmplogsrv); if (!node) { memprintf(errmsg, "proxy '%s': out of memory", curproxy->id); return 1; } - memcpy(node, tmplogsrv, sizeof(struct logsrv)); - node->ref = tmplogsrv->ref; - LIST_INIT(&node->list); LIST_APPEND(&curproxy->logsrvs, &node->list); - node->ring_name = tmplogsrv->ring_name ? strdup(tmplogsrv->ring_name) : NULL; - node->conf.file = strdup(tmplogsrv->conf.file); - node->conf.line = tmplogsrv->conf.line; } curproxy->conf.uniqueid_format_string = defproxy->conf.uniqueid_format_string; -- 2.39.5