]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: server: introduce srv_alloc()/srv_free() to alloc/free a server
authorWilly Tarreau <w@1wt.eu>
Wed, 13 Aug 2025 09:52:59 +0000 (11:52 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 13 Aug 2025 15:37:11 +0000 (17:37 +0200)
It happens that we free servers at various places in the code, both
on error paths and at runtime thanks to the "server delete" feature. In
order to switch to an aligned struct, we'll need to change the calloc()
and free() calls. Let's first spot them and switch them to srv_alloc()
and srv_free() instead of using calloc() and either free() or ha_free().
An easy trap to fall into is that some of them are default-server
entries. The new srv_free() function also resets the pointer like
ha_free() does.

This was done by running the following coccinelle script all over the
code:

  @@
  struct server *srv;
  @@
  (
  - free(srv)
  + srv_free(&srv)
  |
  - ha_free(&srv)
  + srv_free(&srv)
  )
  @@
  struct server *srv;
  expression e1;
  expression e2;
  @@
  (
  - srv = malloc(e1)
  + srv = srv_alloc()
  |
  - srv = calloc(e1, e2)
  + srv = srv_alloc()
  )

This is marked medium because despite spotting all call places, we can
never rule out the possibility that some out-of-tree patches would
allocate their own servers and continue to use the old API... at their
own risk.

include/haproxy/server.h
src/cli.c
src/proxy.c
src/server.c

index 19f0017fd4eb0d66f9d95969c6abb6d515ccf521..dc0e263c87e9cf4011c5df1a76592c67a88ef78a 100644 (file)
@@ -179,6 +179,23 @@ void srv_set_dyncookie(struct server *s);
 int srv_check_reuse_ws(struct server *srv);
 const struct mux_ops *srv_get_ws_proto(struct server *srv);
 
+/* allocate a new server and return it (or NULL on failure). The structure is
+ * zeroed.
+ */
+static inline struct server *srv_alloc(void)
+{
+       struct server *srv;
+
+       srv = calloc(1, sizeof(*srv));
+       return srv;
+}
+
+/* free a previously allocated server an nullifies the pointer */
+static inline void srv_free(struct server **srv_ptr)
+{
+       ha_free(srv_ptr);
+}
+
 /* increase the number of cumulated streams on the designated server */
 static inline void srv_inc_sess_ctr(struct server *s)
 {
index 9895b819ea1fb8fe9602319bc53044bd15105d2e..09a356c00bddf588fc647eab51473e9e552d8883 100644 (file)
--- a/src/cli.c
+++ b/src/cli.c
@@ -3726,7 +3726,7 @@ error:
        list_for_each_entry(child, &proc_list, list) {
                free((char *)child->srv->conf.file); /* cast because of const char *  */
                free(child->srv->id);
-               ha_free(&child->srv);
+               srv_free(&child->srv);
        }
        free(msg);
 
index caa33be2f6c60b34fed8ab661beb5f27ca91b844..2bcd87f6f235c09309ac24f07839a9cfdd65e35f 100644 (file)
@@ -388,7 +388,7 @@ void deinit_proxy(struct proxy *p)
         */
        if (p->defsrv) {
                srv_free_params(p->defsrv);
-               ha_free(&p->defsrv);
+               srv_free(&p->defsrv);
        }
 
        if (p->lbprm.proxy_deinit)
@@ -1560,7 +1560,7 @@ void proxy_free_defaults(struct proxy *defproxy)
        if (defproxy->defsrv)
                ha_free((char **)&defproxy->defsrv->conf.file);
        ha_free(&defproxy->defbe.name);
-       ha_free(&defproxy->defsrv);
+       srv_free(&defproxy->defsrv);
 
        h = defproxy->req_cap;
        while (h) {
@@ -1708,7 +1708,7 @@ int setup_new_proxy(struct proxy *px, const char *name, unsigned int cap, char *
        if (name)
                memprintf(errmsg, "proxy '%s': %s", name, *errmsg);
 
-       ha_free(&px->defsrv);
+       srv_free(&px->defsrv);
        ha_free(&px->id);
        counters_fe_shared_drop(&px->fe_counters.shared);
        counters_be_shared_drop(&px->be_counters.shared);
@@ -1740,7 +1740,7 @@ struct proxy *alloc_new_proxy(const char *name, unsigned int cap, char **errmsg)
         * quitting.
         */
        if (curproxy)
-               free(curproxy->defsrv);
+               srv_free(&curproxy->defsrv);
        free(curproxy);
        return NULL;
 }
@@ -1802,7 +1802,7 @@ static int proxy_defproxy_cpy(struct proxy *curproxy, const struct proxy *defpro
                         * none allocated yet in the current proxy so we have
                         * to allocate and pre-initialize it right now.
                         */
-                       curproxy->defsrv = calloc(1, sizeof(*curproxy->defsrv));
+                       curproxy->defsrv = srv_alloc();
                        if (!curproxy->defsrv) {
                                memprintf(errmsg, "proxy '%s': out of memory allocating default-server", curproxy->id);
                                return 1;
@@ -2833,7 +2833,7 @@ static int post_section_px_cleanup()
 
                if (curproxy->defsrv) {
                        ha_free((char **)&curproxy->defsrv->conf.file);
-                       ha_free(&curproxy->defsrv);
+                       srv_free(&curproxy->defsrv);
                }
        }
        return 0;
index c299d202bf410caf18947ef271fa87c1c18edfd5..3d270e8c2dc1c841c0939254fda70a0aa81da383 100644 (file)
@@ -3042,7 +3042,7 @@ struct server *new_server(struct proxy *proxy)
 {
        struct server *srv;
 
-       srv = calloc(1, sizeof *srv);
+       srv = srv_alloc();
        if (!srv)
                return NULL;
 
@@ -3202,7 +3202,7 @@ struct server *srv_drop(struct server *srv)
 
        EXTRA_COUNTERS_FREE(srv->extra_counters);
 
-       ha_free(&srv);
+       srv_free(&srv);
 
  end:
        return next;
@@ -3354,7 +3354,7 @@ static int _srv_parse_tmpl_init(struct server *srv, struct proxy *px)
                free_check(&newsrv->check);
                MT_LIST_DELETE(&newsrv->global_list);
        }
-       free(newsrv);
+       srv_free(&newsrv);
        return i - srv->tmpl_info.nb_low;
 }
 
@@ -3712,7 +3712,7 @@ static int _srv_parse_init(struct server **srv, char **args, int *cur_arg,
                 */
                newsrv = curproxy->defsrv;
                if (!newsrv) {
-                       newsrv = calloc(1, sizeof(*newsrv));
+                       newsrv = srv_alloc();
                        if (!newsrv) {
                                ha_alert("out of memory.\n");
                                err_code |= ERR_ALERT | ERR_ABORT;