From 3b06eaec8659d24aebe653e02bbdf1084e6dfd32 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Fri, 12 Feb 2021 09:15:16 +0100 Subject: [PATCH] MEDIUM: proxy: only take defaults when a default proxy is passed. The proxy initialization code relies on three phases, allocation, pre-initialization, and assignments from defaults. This last part is entirely taken from the defaults proxy when arguments are set. This sensibly complexifies the initialization code as it requires to always have a default proxy. This patch instead first applies the original default settings on a proxy, and then uses those from a default proxy only if one such is used. This will allow to initialize a proxy out of any default proxy while still using valid defaults. A careful inspection of the function showed that only 4 fields used to be set regardless of the default proxy, and those were moved to init_new_proxy() where they ought to have been in the first place. --- src/proxy.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/proxy.c b/src/proxy.c index 394ba14e74..6a1c9dff96 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -1037,6 +1037,11 @@ void init_new_proxy(struct proxy *p) LIST_INIT(&p->filter_configs); LIST_INIT(&p->tcpcheck_rules.preset_vars); + p->defsrv.id = "default-server"; + p->conf.used_listener_id = EB_ROOT; + p->conf.used_server_id = EB_ROOT; + p->used_server_addr = EB_ROOT_UNIQUE; + /* Timeouts are defined as -1 */ proxy_reset_timeouts(p); p->tcp_rep.inspect_delay = TICK_ETERNITY; @@ -1097,7 +1102,8 @@ void proxy_preset_defaults(struct proxy *defproxy) /* Allocates a new proxy of type found at position , * preset it from the defaults of and returns it. Un case of error, * an alert is printed and NULL is returned. If is not NULL, an error - * message will be returned there in case of fatal error. + * message will be returned there in case of fatal error. If is NULL, + * the documented default settings will be used instead. */ struct proxy *alloc_new_proxy(const char *name, unsigned int cap, const char *file, int linenum, const struct proxy *defproxy, char **errmsg) { @@ -1118,9 +1124,13 @@ struct proxy *alloc_new_proxy(const char *name, unsigned int cap, const char *fi curproxy->cap = cap; proxy_store_name(curproxy); - /* set default values */ + if (!defproxy) { + proxy_preset_defaults(curproxy); + goto done; + } + + /* set default values from the specified default proxy */ memcpy(&curproxy->defsrv, &defproxy->defsrv, sizeof(curproxy->defsrv)); - curproxy->defsrv.id = "default-server"; curproxy->disabled = defproxy->disabled; curproxy->options = defproxy->options; @@ -1334,9 +1344,6 @@ struct proxy *alloc_new_proxy(const char *name, unsigned int cap, const char *fi } curproxy->grace = defproxy->grace; - curproxy->conf.used_listener_id = EB_ROOT; - curproxy->conf.used_server_id = EB_ROOT; - curproxy->used_server_addr = EB_ROOT_UNIQUE; if (defproxy->check_path) curproxy->check_path = strdup(defproxy->check_path); @@ -1353,6 +1360,8 @@ struct proxy *alloc_new_proxy(const char *name, unsigned int cap, const char *fi curproxy->email_alert.myhostname = strdup(defproxy->email_alert.myhostname); curproxy->email_alert.level = defproxy->email_alert.level; curproxy->email_alert.set = defproxy->email_alert.set; + + done: return curproxy; fail: /* Note: in case of fatal error here, we WILL make valgrind unhappy,