MINOR: proxy: add checks for defsrv's validity

Now we only copy the default server's settings if such a default server
exists, otherwise we only initialize it. At the moment it always exists.

The change is mostly performed in srv_settings_cpy() since that's where
each caller passes through, and there's no point duplicating that test
everywhere.
This commit is contained in:
Willy Tarreau 2025-07-10 17:03:13 +02:00
parent 4ac28f07d0
commit 76828d4120
2 changed files with 31 additions and 4 deletions

View File

@ -1557,7 +1557,8 @@ void proxy_free_defaults(struct proxy *defproxy)
proxy_free_common(defproxy);
/* default proxy specific cleanup */
ha_free((char **)&defproxy->defsrv->conf.file);
if (defproxy->defsrv)
ha_free((char **)&defproxy->defsrv->conf.file);
ha_free(&defproxy->defbe.name);
ha_free(&defproxy->defsrv);
@ -1806,7 +1807,8 @@ static int proxy_defproxy_cpy(struct proxy *curproxy, const struct proxy *defpro
struct eb32_node *node;
/* set default values from the specified default proxy */
srv_settings_cpy(curproxy->defsrv, defproxy->defsrv, 0);
if (curproxy->defsrv)
srv_settings_cpy(curproxy->defsrv, defproxy->defsrv, 0);
curproxy->flags = (defproxy->flags & PR_FL_DISABLED); /* Only inherit from disabled flag */
curproxy->options = defproxy->options;

View File

@ -2855,7 +2855,11 @@ void srv_settings_init(struct server *srv)
/*
* Copy <src> server settings to <srv> server allocating
* everything needed.
* everything needed. This is used to pre-initialize a server from
* default-server settings. If the source is NULL (i.e. no defsrv)
* then we fall back to srv_settings_init() to pre-initialize a
* clean new server.
*
* This function is not supposed to be called at any time, but only
* during server settings parsing or during server allocations from
* a server template, and just after having calloc()'ed a new server.
@ -2868,6 +2872,11 @@ void srv_settings_cpy(struct server *srv, const struct server *src, int srv_tmpl
{
struct srv_pp_tlv_list *srv_tlv = NULL, *new_srv_tlv = NULL;
if (!src) {
srv_settings_init(srv);
return;
}
/* Connection source settings copy */
srv_conn_src_cpy(srv, src);
@ -3698,7 +3707,23 @@ static int _srv_parse_init(struct server **srv, char **args, int *cur_arg,
HA_SPIN_INIT(&newsrv->lock);
}
else {
*srv = newsrv = curproxy->defsrv;
/* This is a "default-server" line. Let's make certain the
* current proxy's default server exists, otherwise it's
* time to allocate it now.
*/
newsrv = curproxy->defsrv;
if (!newsrv) {
newsrv = calloc(1, sizeof(*newsrv));
if (!newsrv) {
ha_alert("out of memory.\n");
err_code |= ERR_ALERT | ERR_ABORT;
goto out;
}
newsrv->id = "default-server";
srv_settings_init(newsrv);
curproxy->defsrv = newsrv;
}
*srv = newsrv;
*cur_arg = 1;
}