]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: proxy: add checks for defsrv's validity
authorWilly Tarreau <w@1wt.eu>
Thu, 10 Jul 2025 15:03:13 +0000 (17:03 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 15 Jul 2025 08:36:58 +0000 (10:36 +0200)
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.

src/proxy.c
src/server.c

index deef09b02d85b39481d6862b4707f96f9b4765fa..0eea5de2db2810bdc975a48421b876b95ce3495e 100644 (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;
index acf397e55ef83dd0f07fe424623e50bd0a0e43a3..bbf7f8287a60545254612adc5962159fee8839ae 100644 (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;
        }