From: Willy Tarreau Date: Thu, 10 Jul 2025 15:03:13 +0000 (+0200) Subject: MINOR: proxy: add checks for defsrv's validity X-Git-Tag: v3.3-dev4~85 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=76828d4120c9b6f4cad75d84f201e5dd5b023be4;p=thirdparty%2Fhaproxy.git 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. --- diff --git a/src/proxy.c b/src/proxy.c index deef09b02..0eea5de2d 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -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; diff --git a/src/server.c b/src/server.c index acf397e55..bbf7f8287 100644 --- a/src/server.c +++ b/src/server.c @@ -2855,7 +2855,11 @@ void srv_settings_init(struct server *srv) /* * Copy server settings to 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; }